diff --git a/src/cmd/linuxkit/Makefile b/src/cmd/linuxkit/Makefile index 87bc06d7f..3b2694751 100644 --- a/src/cmd/linuxkit/Makefile +++ b/src/cmd/linuxkit/Makefile @@ -1,5 +1,24 @@ -VERSION?="v0.0-dev" +# determine the version we save in the build binary +# we always include the git commit. +# the version is the current semver if it this commit matches the tag, +# else it is the following: -- +# if the git tree is dirty, append "-dirty" +# most recent commit GIT_COMMIT=$(shell git rev-list -1 HEAD) +# whether or not it is dirty, i.e. has uncommitted changes +GIT_DIRTY=$(shell git update-index -q --refresh && git diff-index --quiet HEAD -- . || echo "-dirty") +# most recent tag, might or might not point to GIT_COMMIT +GIT_TAG=$(shell git describe --tags --match="v*") +# include the possible "-dirty" suffix +VERSION=$(GIT_TAG)$(GIT_DIRTY) + +report: + @echo "VERSION: $(VERSION)" + @echo "GIT_COMMIT: $(GIT_COMMIT)" + @echo "GIT_DIRTY: $(GIT_DIRTY)" + @echo "GIT_TAG: $(GIT_TAG)" + @echo "VERSION: $(VERSION)" + GO_COMPILE?=linuxkit/go-compile:c97703655e8510b7257ffc57f25e40337b0f0813 export GO_FLAGS=-mod=vendor diff --git a/src/cmd/linuxkit/version.go b/src/cmd/linuxkit/version.go index bacc680ee..a0b5b2db0 100644 --- a/src/cmd/linuxkit/version.go +++ b/src/cmd/linuxkit/version.go @@ -10,11 +10,22 @@ import ( ) func versionCmd() *cobra.Command { + var short, commit bool cmd := &cobra.Command{ Use: "version", Short: "report the version of linuxkit", - Long: `Report the version of linuxkit.`, + Long: `Report the version of linuxkit. + Run with option --short to print just the version number. + Run with option --commit to print just the git commit.`, RunE: func(cmd *cobra.Command, args []string) error { + if short { + fmt.Println(version.Version) + return nil + } + if commit { + fmt.Println(version.GitCommit) + return nil + } fmt.Printf("%s version %s\n", filepath.Base(os.Args[0]), version.Version) if version.GitCommit != "" { fmt.Printf("commit: %s\n", version.GitCommit) @@ -22,6 +33,8 @@ func versionCmd() *cobra.Command { return nil }, } + cmd.Flags().BoolVar(&short, "short", false, "print just the version number") + cmd.Flags().BoolVar(&commit, "commit", false, "print just the commit") return cmd }