Skip to content

Commit 41c60bc

Browse files
committed
Add version flag to generator
ref: #118 ref: https://issues.redhat.com/browse/ACM-5803 Signed-off-by: Dale Haiducek <[email protected]>
1 parent ab49eaf commit 41c60bc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ jobs:
2222

2323
- name: Build project
2424
run: |
25+
GENERATOR_VERSION="$(cat VERSION.txt)"
26+
if [[ "${{ github.ref_name }}" != "${GENERATOR_VERSION}" ]]; then
27+
echo "ERROR: Tag ${{ github.ref_name }} does not match ${GENERATOR_VERSION} specified in VERSION.txt."
28+
exit 1
29+
fi
2530
make build-release
2631
2732
- name: Release

cmd/PolicyGenerator/VERSION.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v1.12.2

cmd/PolicyGenerator/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,48 @@ package main
22

33
import (
44
"bytes"
5+
_ "embed"
56
"fmt"
67
"os"
8+
runtimeDebug "runtime/debug"
79

810
"github.com/spf13/pflag"
911
"open-cluster-management.io/policy-generator-plugin/internal"
1012
)
1113

14+
//go:embed VERSION.txt
15+
var version []byte
1216
var debug = false
1317

18+
// Gather the Git commit
19+
var gitCommit = func() string {
20+
if info, ok := runtimeDebug.ReadBuildInfo(); ok {
21+
for _, setting := range info.Settings {
22+
if setting.Key == "vcs.revision" {
23+
return setting.Value
24+
}
25+
}
26+
}
27+
28+
return ""
29+
}()
30+
1431
func main() {
1532
// Parse command input
1633
debugFlag := pflag.Bool("debug", false, "Print the stack trace with error messages")
34+
versionFlag := pflag.Bool("version", false, "Print the version of the generator")
1735
pflag.Parse()
1836

37+
if *versionFlag {
38+
versionStr := string(version)
39+
if gitCommit != "" {
40+
versionStr = fmt.Sprintf("%s-%s", versionStr, gitCommit)
41+
}
42+
//nolint:forbidigo
43+
fmt.Println(versionStr)
44+
os.Exit(0)
45+
}
46+
1947
debug = *debugFlag
2048

2149
// Collect and parse PolicyGeneratorConfig file paths

0 commit comments

Comments
 (0)