Skip to content

Commit 474b260

Browse files
committed
feat: use git tags for versioning via ldflags instead of hardcoded version
Replace the hardcoded version string with build-time injection using -ldflags. Version, build time, and git commit are now derived from git tags and injected by both the Makefile and GoReleaser.
1 parent a83357f commit 474b260

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

.goreleaser.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ builds:
3232
# - windows_arm64
3333
# - android_arm64
3434
ldflags:
35-
- "-s -w -X 'main.version={{ .Tag }}'"
35+
- -s -w
36+
- -X main.version={{.Version}}
37+
- -X main.buildTime={{.Date}}
38+
- -X main.gitCommit={{.Commit}}
3639

3740
upx:
3841
- enabled: true

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
NAME=greyproxy
22
BINDIR=bin
3-
VERSION=$(shell cat cmd/greyproxy/version.go | grep 'version =' | sed 's/.*\"\(.*\)\".*/\1/g')
4-
GOBUILD=CGO_ENABLED=0 go build --ldflags="-s -w" -v -x -a
3+
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
4+
BUILD_TIME=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
5+
GIT_COMMIT=$(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
6+
LDFLAGS=-s -w -X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME) -X main.gitCommit=$(GIT_COMMIT)
7+
GOBUILD=CGO_ENABLED=0 go build --ldflags="$(LDFLAGS)" -v -x -a
58
GOFILES=cmd/greyproxy/*.go
69

710
PLATFORM_LIST = \

cmd/greyproxy/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func parseFlags() {
9595
flag.Parse()
9696

9797
if printVersion {
98-
fmt.Fprintf(os.Stdout, "greyproxy %s (%s %s/%s)\n",
99-
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
98+
fmt.Fprintf(os.Stdout, "greyproxy %s (%s %s/%s)\n built: %s\n commit: %s\n",
99+
version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildTime, gitCommit)
100100
os.Exit(0)
101101
}
102102
}
@@ -143,8 +143,8 @@ func main() {
143143
handleUninstall(os.Args[2:])
144144

145145
case "-V", "--version":
146-
fmt.Fprintf(os.Stdout, "greyproxy %s (%s %s/%s)\n",
147-
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
146+
fmt.Fprintf(os.Stdout, "greyproxy %s (%s %s/%s)\n built: %s\n commit: %s\n",
147+
version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildTime, gitCommit)
148148

149149
default:
150150
printUsage()

cmd/greyproxy/version.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package main
22

3+
// Build-time variables (set via -ldflags)
34
var (
4-
version = "0.2.0"
5+
version = "dev"
6+
buildTime = "unknown"
7+
gitCommit = "unknown"
58
)

0 commit comments

Comments
 (0)