From 0bcba8d1d211bd4deb4812a3861f8728eefad5ea Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 25 Jun 2020 13:50:16 +0200 Subject: [PATCH] =?UTF-8?q?Set=20LD=5FFLAG=20version=20from=20git=20tag.?= =?UTF-8?q?=20Displayed=20version=20will=20be=20(default=20behaviour=20of?= =?UTF-8?q?=20git=20describe=20=E2=80=94tags):=20*=20release=20version=20:?= =?UTF-8?q?=20if=20tag=20`v0.1.2`=20is=20exactly=20on=20the=20built=20comm?= =?UTF-8?q?it=20:=20`0.1.2`=20(strip=20v=20when=20displaying=20`docker=20v?= =?UTF-8?q?ersion`)=20*=20dev=20version=20:=20if=20tag=20`v0.1.2`=20is=203?= =?UTF-8?q?=20commits=20before,=20and=20we=20are=20on=20commit=20`xyz`=20:?= =?UTF-8?q?=20`0.1.2-3-xyz`=20*=20will=20only=20match=20tags=20starting=20?= =?UTF-8?q?with=20`v[0-9]`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builder.Makefile | 14 ++++++++------ cli/cmd/version.go | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/builder.Makefile b/builder.Makefile index 6527b575e..c76668106 100644 --- a/builder.Makefile +++ b/builder.Makefile @@ -23,7 +23,9 @@ ifeq ($(GOOS),windows) endif STATIC_FLAGS=CGO_ENABLED=0 -LDFLAGS:="-s -w" +GIT_TAG=$(shell git describe --tags --match "v[0-9]*") + +LDFLAGS="-s -w -X main.version=${GIT_TAG}" GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS) BINARY?=bin/docker @@ -37,18 +39,18 @@ endif all: cli protos: - @protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS} + protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS} cli: GOOS=${GOOS} GOARCH=${GOARCH} $(GO_BUILD) $(TAGS) -o $(BINARY_WITH_EXTENSION) ./cli cross: - @GOOS=linux GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-linux-amd64 ./cli - @GOOS=darwin GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-darwin-amd64 ./cli - @GOOS=windows GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-windows-amd64.exe ./cli + GOOS=linux GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-linux-amd64 ./cli + GOOS=darwin GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-darwin-amd64 ./cli + GOOS=windows GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-windows-amd64.exe ./cli test: - @go test $(TAGS) -cover $(shell go list ./... | grep -vE 'e2e') + go test $(TAGS) -cover $(shell go list ./... | grep -vE 'e2e') lint: golangci-lint run --timeout 10m0s ./... diff --git a/cli/cmd/version.go b/cli/cmd/version.go index ba657d895..285f169df 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -46,6 +46,7 @@ func VersionCommand(version string) *cobra.Command { } func runVersion(cmd *cobra.Command, version string) error { + displayedVersion := strings.TrimPrefix(version, "v") versionResult, _ := mobycli.ExecSilent(cmd.Context()) // we don't want to fail on error, there is an error if the engine is not available but it displays client version info // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display @@ -53,6 +54,6 @@ func runVersion(cmd *cobra.Command, version string) error { return mobycli.ExecCmd(cmd) } var s string = string(versionResult) - fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+version+"\n Version:", 1)) + fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+displayedVersion+"\n Version:", 1)) return nil }