Skip to content

Commit 9053416

Browse files
committed
feat: add go version, commit hash and build time to the version info
and pass all these via the compile time variable definition (ldflags)
1 parent 9ab9b8e commit 9053416

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
APPNAME = commit-msg
1+
APPNAME := commit-msg
22
LDFLAGS += -s -w
33

44
.PHONY: all build upx
55

66
all: build upx
77

88
build:
9-
go build -trimpath -ldflags '$(LDFLAGS)'
9+
go build -trimpath -ldflags "$(LDFLAGS) -X 'main.version=$(TAG)' -X 'main.goVersion=$(shell go version)' -X 'main.commitHash=$(shell git show -s --format=%H)' -X 'main.buildTime=$(shell date "+%Y-%m-%d %T%z")'"
1010

1111
upx:
1212
upx `find -maxdepth 1 -name "$(APPNAME)" -o -name "$(APPNAME).exe"`

main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"github.com/JayceChant/commit-msg/validator"
1010
)
1111

12-
const (
13-
versionStr = "v0.2.0"
14-
)
15-
1612
var (
1713
versionFlag = flag.Bool("version", false, "")
14+
version string
15+
goVersion string
16+
commitHash string
17+
buildTime string
1818
)
1919

2020
func main() {
@@ -33,5 +33,14 @@ func main() {
3333
}
3434

3535
func printVersion(cmd string) {
36-
fmt.Printf("%s %s\n", filepath.Base(cmd), versionStr)
36+
fmt.Println(filepath.Base(cmd), version)
37+
if goVersion != "" {
38+
fmt.Println(goVersion)
39+
}
40+
if commitHash != "" {
41+
fmt.Println("commit hash :", commitHash)
42+
}
43+
if buildTime != "" {
44+
fmt.Println("build at :", buildTime)
45+
}
3746
}

0 commit comments

Comments
 (0)