Skip to content

Commit

Permalink
Kubetest2 Version Addition
Browse files Browse the repository at this point in the history
- Uses ldflags to capture HEAD rev in short form.
- Prints built commit version by default
- Adds --version support
- Fixes 108
  • Loading branch information
supriya-premkumar committed Mar 12, 2021
1 parent 9230b4e commit aec704c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) ./...
Expand Down
8 changes: 8 additions & 0 deletions pkg/app/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit aec704c

Please sign in to comment.