-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
54 lines (42 loc) · 1.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
)
// Version value, injected via go build `ldflags` at build time
var version = "dev"
func main() {
info, ok := debug.ReadBuildInfo()
if !ok {
panic("we should have been able to retrieve info from 'runtime/debug#ReadBuildInfo'")
}
rootCmd.Version = computeVersionString(version, info.Settings)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func computeVersionString(version string, settings []debug.BuildSetting) string {
commit := findSetting("vcs.revision", settings)
date := findSetting("vcs.time", settings)
var labels []string
if len(commit) >= 7 {
labels = append(labels, fmt.Sprintf("Commit %s", commit[0:7]))
}
if date != "" {
labels = append(labels, fmt.Sprintf("Built %s", date))
}
if len(labels) == 0 {
return version
}
return fmt.Sprintf("%s (%s)", version, strings.Join(labels, ", "))
}
func findSetting(key string, settings []debug.BuildSetting) (value string) {
for _, setting := range settings {
if setting.Key == key {
return setting.Value
}
}
return ""
}