diff --git a/Makefile b/Makefile index 274b0dc2..8a3b2cb3 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,8 @@ REPO_ROOT:=$(shell pwd) export REPO_ROOT OUT_DIR=$(REPO_ROOT)/bin +# record the source commit in the binary, overridable +COMMIT?=$(shell git rev-parse --short HEAD 2>/dev/null) INSTALL?=install # make install will place binaries here # the default path attempts to mimic go install @@ -47,7 +49,7 @@ SPACE:=$(subst ,, ) SHELL:=env PATH=$(subst $(SPACE),\$(SPACE),$(PATH)) $(SHELL) # ============================================================================== # flags for reproducible go builds -BUILD_FLAGS?=-trimpath -ldflags="-buildid=" +BUILD_FLAGS?=-trimpath -ldflags="-buildid= -X=sigs.k8s.io/kubetest2/pkg/app/shim.GitTag=$(COMMIT)" build-all: go build -v $(BUILD_FLAGS) ./... diff --git a/pkg/app/shim/shim.go b/pkg/app/shim/shim.go index bee6e1b5..3269ed42 100644 --- a/pkg/app/shim/shim.go +++ b/pkg/app/shim/shim.go @@ -26,6 +26,8 @@ import ( "sigs.k8s.io/kubetest2/pkg/process" ) +var GitTag string // initializing default zero value. ldflags will populate this during build time. + // Main implements the kubetest2 root binary entrypoint func Main() { if err := Run(); err != nil { @@ -66,6 +68,7 @@ func NewCommand() *cobra.Command { // runE implements the actual command logic func runE(cmd *cobra.Command, args []string) error { + cmd.Printf("Running %s version: %s\n", BinaryName, GitTag) // there should be at least one argument (the deployer) unless the user // is asking for help on the shim itself if len(args) < 1 { @@ -77,11 +80,16 @@ func runE(cmd *cobra.Command, args []string) error { // check for -h, --help flags := pflag.NewFlagSet(BinaryName, pflag.ContinueOnError) help := flags.BoolP("help", "h", false, "") + version := flags.BoolP("version", "v", false, fmt.Sprintf("prints %s version", BinaryName)) // we don't care about errors, only if -h / --help was set _ = flags.Parse(args) if *help { return cmd.Help() } + if *version { + cmd.Printf("Running %s version: %s\n", BinaryName, GitTag) + return nil + } } // otherwise find and execute the deployer with the remaining arguments