Skip to content

Commit 2c4b968

Browse files
feat(config): add Version, FullVersion
1 parent 40e17f2 commit 2c4b968

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

config/version.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"runtime/debug"
6+
)
7+
8+
// Build constants
9+
// all variables are set during build
10+
var (
11+
version string
12+
commit string
13+
buildTime string
14+
)
15+
16+
// Version returns short version number of the commitlint
17+
func Version() string {
18+
return formShortVersion()
19+
}
20+
21+
// FullVersion returns version number with hash and build time of the commitlint
22+
func FullVersion() string {
23+
return formFullVersion()
24+
}
25+
26+
func formShortVersion() string {
27+
if buildTime != "" {
28+
return version
29+
}
30+
31+
info, ok := debug.ReadBuildInfo()
32+
if !ok {
33+
return "v0"
34+
}
35+
return info.Main.Version
36+
}
37+
38+
func formFullVersion() string {
39+
versionTmpl := "%s - built from %s on %s"
40+
41+
if buildTime != "" {
42+
return fmt.Sprintf(versionTmpl, version, commit, buildTime)
43+
}
44+
45+
info, ok := debug.ReadBuildInfo()
46+
if !ok {
47+
return fmt.Sprintf(versionTmpl, "master", "unknown", "unknown")
48+
}
49+
50+
var commitInfo string
51+
if info.Main.Sum == "" {
52+
commitInfo = "(" + "checksum: unknown)"
53+
} else {
54+
commitInfo = "(" + "checksum: " + info.Main.Sum + ")"
55+
}
56+
return fmt.Sprintf(versionTmpl, info.Main.Version, commitInfo, "unknown")
57+
}

0 commit comments

Comments
 (0)