forked from samuelkarp/runj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
60 lines (54 loc) · 1.1 KB
/
version.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
55
56
57
58
59
60
package runj
import (
_ "embed" // use to embed VERSION and REV_OVERRIDE files
"fmt"
"runtime/debug"
"strings"
)
var (
//go:embed VERSION
version string
//go:embed REV_OVERRIDE
revOverride string
rendered string
)
func init() {
rendered = render()
}
// Version returns a version string for runj and its dependencies
func Version() string {
return rendered
}
func render() string {
version = strings.TrimSpace(version)
revOverride = strings.TrimSpace(revOverride)
bi, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
revision := ""
modified := false
for _, setting := range bi.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
if modified {
revision = revision + "*"
}
if revOverride != "" {
revision = revOverride
}
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("%s (%s)\n", version, revision))
sb.WriteString("go: " + bi.GoVersion)
for _, dep := range bi.Deps {
sb.WriteString(fmt.Sprintf("\n%s: %s", dep.Path, dep.Version))
}
return sb.String()
}