From 81fda274404ade88532fa6b8a8bc8af920d852ce Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Wed, 13 Sep 2017 10:16:16 -0400 Subject: [PATCH] Tweak version display Signed-off-by: Andy Goldstein --- Makefile | 12 +++++++++--- pkg/buildinfo/version.go | 22 ++++++++++++++-------- pkg/cmd/version/version.go | 4 +++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ac27ccf81..e0c995316 100644 --- a/Makefile +++ b/Makefile @@ -21,17 +21,23 @@ OUTPUT_DIR = $(ROOT_DIR)/_output BIN_DIR = $(OUTPUT_DIR)/bin GIT_SHA=$(shell git rev-parse --short HEAD) GIT_DIRTY=$(shell git status --porcelain $(ROOT_DIR) 2> /dev/null) -ifneq ($(GIT_DIRTY),) - GIT_SHA := $(GIT_SHA)-dirty +ifeq ($(GIT_DIRTY),) + GIT_TREE_STATE := clean +else + GIT_TREE_STATE := dirty endif # docker related vars DOCKER ?= docker REGISTRY ?= gcr.io/heptio-images BUILD_IMAGE ?= gcr.io/heptio-images/golang:1.8-alpine3.6 +LDFLAGS := -X $(GOTARGET)/pkg/buildinfo.Version=$(VERSION) +LDFLAGS += -X $(GOTARGET)/pkg/buildinfo.DockerImage=$(REGISTRY)/$(PROJECT) +LDFLAGS += -X $(GOTARGET)/pkg/buildinfo.GitSHA=$(GIT_SHA) +LDFLAGS += -X $(GOTARGET)/pkg/buildinfo.GitTreeState=$(GIT_TREE_STATE) # go build -i installs compiled packages so they can be reused later. # This speeds up recompiles. -BUILDCMD = go build -i -v -ldflags "-X $(GOTARGET)/pkg/buildinfo.Version=$(VERSION) -X $(GOTARGET)/pkg/buildinfo.DockerImage=$(REGISTRY)/$(PROJECT) -X $(GOTARGET)/pkg/buildinfo.GitSHA=$(GIT_SHA)" +BUILDCMD = go build -i -v -ldflags "$(LDFLAGS)" BUILDMNT = /go/src/$(GOTARGET) EXTRA_MNTS ?= diff --git a/pkg/buildinfo/version.go b/pkg/buildinfo/version.go index 8d2989168..e23470db0 100644 --- a/pkg/buildinfo/version.go +++ b/pkg/buildinfo/version.go @@ -14,17 +14,23 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package buildinfo holds build-time information like the sonobuoy version. +// Package buildinfo holds build-time information like the ark version. // This is a separate package so that other packages can import it without // worrying about introducing circular dependencies. package buildinfo -// Version is the current version of Ark, set by the go linker's -X flag at build time. -var Version string +var ( + // Version is the current version of Ark, set by the go linker's -X flag at build time. + Version string -// DockerImage is the full path to the docker image for this build, for example -// gcr.io/heptio-images/ark. -var DockerImage string + // DockerImage is the full path to the docker image for this build, for example + // gcr.io/heptio-images/ark. + DockerImage string -// GitSHA is the actual commit that is being built, set by the go linker's -X flag at build time. -var GitSHA string + // GitSHA is the actual commit that is being built, set by the go linker's -X flag at build time. + GitSHA string + + // GitTreeState indicates if the git tree is clean or dirty, set by the go linker's -X flag at build + // time. + GitTreeState string +) diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go index 8eafe4329..5c9556439 100644 --- a/pkg/cmd/version/version.go +++ b/pkg/cmd/version/version.go @@ -29,7 +29,9 @@ func NewCommand() *cobra.Command { Use: "version", Short: "Print the ark version and associated image", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("Version: [%s] - [%s]\n", buildinfo.Version, buildinfo.GitSHA) + fmt.Printf("Version: %s\n", buildinfo.Version) + fmt.Printf("Git commit: %s\n", buildinfo.GitSHA) + fmt.Printf("Git tree state: %s\n", buildinfo.GitTreeState) fmt.Println("Configured docker image:", buildinfo.DockerImage) }, }