File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -45,14 +45,24 @@ clean:
4545# ###########################################################
4646# build section
4747# ###########################################################
48+ GIT_VERSION := $(shell git describe --dirty 2>/dev/null)
49+ ifndef GIT_VERSION
50+ GIT_BRANCH := $(shell git branch --show-current)
51+ ifdef GIT_BRANCH
52+ GIT_VERSION := $(GIT_BRANCH ) -$(shell git rev-parse --short HEAD)
53+ else
54+ GIT_VERSION := $(shell git rev-parse --short HEAD) -dev
55+ endif
56+ endif
57+ GO_LDFLAGS ?= -X 'main.Version=$(GIT_VERSION ) '
4858
4959.PHONY : build
5060build : layout
51- go build -o $(API_PLUGIN_PATH ) / ./cmd/PolicyGenerator
61+ go build -ldflags= " $( GO_LDFLAGS ) " - o $(API_PLUGIN_PATH ) / ./cmd/PolicyGenerator
5262
5363.PHONY : build-binary
5464build-binary :
55- go build ./cmd/PolicyGenerator
65+ go build -ldflags= " $( GO_LDFLAGS ) " ./cmd/PolicyGenerator
5666
5767.PHONY : build-release
5868build-release :
@@ -62,9 +72,9 @@ build-release:
6272 exit 1; \
6373 fi
6474 @mkdir -p build_output
65- GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o build_output/linux-amd64-PolicyGenerator ./cmd/PolicyGenerator
66- GOOS=darwin CGO_ENABLED=0 GOARCH=amd64 go build -o build_output/darwin-amd64-PolicyGenerator ./cmd/PolicyGenerator
67- GOOS=windows CGO_ENABLED=0 GOARCH=amd64 go build -o build_output/windows-amd64-PolicyGenerator.exe ./cmd/PolicyGenerator
75+ GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -ldflags= " $( GO_LDFLAGS ) " - o build_output/linux-amd64-PolicyGenerator ./cmd/PolicyGenerator
76+ GOOS=darwin CGO_ENABLED=0 GOARCH=amd64 go build -ldflags= " $( GO_LDFLAGS ) " - o build_output/darwin-amd64-PolicyGenerator ./cmd/PolicyGenerator
77+ GOOS=windows CGO_ENABLED=0 GOARCH=amd64 go build -ldflags= " $( GO_LDFLAGS ) " - o build_output/windows-amd64-PolicyGenerator.exe ./cmd/PolicyGenerator
6878
6979.PHONY : generate
7080generate :
Original file line number Diff line number Diff line change @@ -4,18 +4,31 @@ import (
44 "bytes"
55 "fmt"
66 "os"
7+ "strings"
78
89 "github.com/spf13/pflag"
910 "open-cluster-management.io/policy-generator-plugin/internal"
1011)
1112
13+ var Version string
14+
1215var debug = false
1316
1417func main () {
1518 // Parse command input
1619 debugFlag := pflag .Bool ("debug" , false , "Print the stack trace with error messages" )
20+ versionFlag := pflag .Bool ("version" , false , "Print the version of the generator" )
1721 pflag .Parse ()
1822
23+ if * versionFlag {
24+ if Version == "" {
25+ Version = "Unversioned binary"
26+ }
27+ //nolint:forbidigo
28+ fmt .Println (strings .TrimSpace (Version ))
29+ os .Exit (0 )
30+ }
31+
1932 debug = * debugFlag
2033
2134 // Collect and parse PolicyGeneratorConfig file paths
You can’t perform that action at this time.
0 commit comments