Skip to content

Commit 3aba757

Browse files
improved buildvars defaults
1 parent 3e6d99a commit 3aba757

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"console": "integratedTerminal",
2424
"hideSystemGoroutines": true,
2525
"buildFlags": [
26-
"-ldflags=-X github.com/toeirei/keymaster/buildvars.Version=development"
26+
"-ldflags=-X github.com/toeirei/keymaster/buildvars.Version=dev"
2727
]
2828
},
2929
{

buildvars/vars.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,41 @@
55
// Package buildvars contains variables injected at build time.
66
package buildvars
77

8+
import (
9+
"runtime/debug"
10+
)
11+
812
// Version is set at link time via `-ldflags -X github.com/toeirei/keymaster/buildvars.Version=...`.
9-
var Version string = "unknown version"
13+
var Version string
14+
15+
func init() {
16+
if Version == "" {
17+
Version = "unknown"
18+
19+
if buildInfo, ok := debug.ReadBuildInfo(); ok {
20+
var vcs, vcsRevision, vcsModified string
21+
for _, setting := range buildInfo.Settings {
22+
switch setting.Key {
23+
case "vcs":
24+
vcs = setting.Value
25+
case "vcs.revision":
26+
vcsRevision = setting.Value
27+
case "vcs.modified":
28+
vcsModified = setting.Value
29+
}
30+
}
31+
32+
if vcs != "" && vcsRevision != "" {
33+
if vcs == "git" {
34+
vcsRevision = vcsRevision[:7]
35+
}
36+
37+
Version = vcs + ":" + vcsRevision
38+
39+
if vcsModified == "true" {
40+
Version += "*"
41+
}
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)