Skip to content

Commit 0ac0913

Browse files
committed
fix version info
1 parent 141945f commit 0ac0913

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
MODULE = $(shell env GO111MODULE=on $(GO) list -m)
22
DATE ?= $(shell date +%FT%T%z)
3-
VERSION ?= $(shell git describe --tags --always --dirty --match="v*" 2> /dev/null || \
3+
VERSION ?= $(shell git describe --tags --always --dirty --match="*" 2> /dev/null || \
44
cat $(CURDIR)/.version 2> /dev/null || echo v0)
5+
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
6+
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
57
PKGS = $(or $(PKG),$(shell env GO111MODULE=on $(GO) list ./...))
68
TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \
79
'{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \
810
$(PKGS))
911
BIN = $(CURDIR)/.bin
10-
LDFLAGS_VERSION = -X main.Version=$(VERSION) -X main.BuildDate=$(DATE) -X main.GitCommit=$(COMMIT) -X main.GitBranch=$(BRANCH)
1112
LINT_CONFIG = $(CURDIR)/.golangci.yaml
1213
PLATFORMS = darwin linux
1314
ARCHITECTURES = amd64 arm64
1415
TARGETOS ?= $(GOOS)
1516
TARGETARCH ?= $(GOARCH)
17+
LDFLAGS_VERSION = -X main.Version=$(VERSION) -X main.BuildDate=$(DATE) -X main.GitCommit=$(COMMIT) -X main.GitBranch=$(BRANCH)
1618

1719
DOCKER = docker
1820
GO = go
@@ -29,7 +31,7 @@ export GOPROXY=https://proxy.golang.org
2931
all: fmt lint test ; $(info $(M) building executable…) @ ## Build program binary
3032
$Q env GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) $(GO) build \
3133
-tags release \
32-
-ldflags "$(LDFLAGS_VERSION)" \
34+
-ldflags "$(LDFLAGS_VERSION) -X main.Platform=$(TARGETOS)/$(TARGETARCH)" \
3335
-o $(BIN)/$(basename $(MODULE)) main.go
3436

3537
# Release for multiple platforms
@@ -42,7 +44,7 @@ platfrom-build: clean lint test ; $(info $(M) building binaries for multiple os/
4244
GOPROXY=$(GOPROXY) CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \
4345
$(GO) build \
4446
-tags release \
45-
-ldflags "$(LDFLAGS_VERSION)" \
47+
-ldflags "$(LDFLAGS_VERSION) -X main.Platform=$(GOOS)/$(GOARCH)" \
4648
-o $(BIN)/$(basename $(MODULE))-$(GOOS)-$(GOARCH) main.go || true)))
4749

4850
# Tools

main.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ import (
2020
)
2121

2222
var (
23-
// Version contains the current version.
24-
version = "dev"
25-
// BuildDate contains a string with the build date.
26-
date = "unknown"
27-
// commit
28-
commit = "dirty"
29-
// buildBy
30-
builtBy = "local"
23+
// Version contains the current Version.
24+
Version = "dev"
25+
// BuildDate contains a string with the build BuildDate.
26+
BuildDate = "unknown"
27+
// GitCommit git commit sha
28+
GitCommit = "dirty"
29+
// GitBranch git branch
30+
GitBranch = "dirty"
31+
// Platform OS/ARCH
32+
Platform = ""
3133
)
3234

3335
func main() {
@@ -42,13 +44,14 @@ func main() {
4244
Name: "secrets-init",
4345
Usage: "enrich environment variables with secrets from secret manager",
4446
Action: mainCmd,
45-
Version: version,
47+
Version: Version,
4648
}
4749
cli.VersionPrinter = func(c *cli.Context) {
48-
fmt.Printf("secrets-init %s\n", version)
49-
fmt.Printf(" build date: %s\n", date)
50-
fmt.Printf(" commit: %s\n", commit)
51-
fmt.Printf(" built by: %s\n", builtBy)
50+
fmt.Printf("version: %s\n", Version)
51+
fmt.Printf(" build date: %s\n", BuildDate)
52+
fmt.Printf(" commit: %s\n", GitCommit)
53+
fmt.Printf(" branch: %s\n", GitBranch)
54+
fmt.Printf(" platform: %s\n", Platform)
5255
fmt.Printf(" built with: %s\n", runtime.Version())
5356
}
5457

0 commit comments

Comments
 (0)