-
Notifications
You must be signed in to change notification settings - Fork 69
/
version.go
125 lines (97 loc) · 2.37 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"runtime/debug"
"time"
)
const (
banner = "Pretender by RedTeam Pentesting"
shortCommitSize = 10
)
var version = "" // this variable can be set during compilation
func buildSettingReader() func(string) (string, bool) {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return func(string) (string, bool) { return "", false }
}
return func(key string) (string, bool) {
for _, setting := range buildInfo.Settings {
if setting.Key == key {
return setting.Value, true
}
}
return "", false
}
}
func shortVersion() string {
fallback := version
if fallback == "" {
fallback = "(unknown version)"
}
readBuildSetting := buildSettingReader()
commit, ok := readBuildSetting("vcs.revision")
if !ok {
return fmt.Sprintf("%s %s", banner, fallback)
}
if len(commit) > shortCommitSize {
commit = commit[:shortCommitSize]
}
dirty, _ := readBuildSetting("vcs.modified")
if version == "" {
vcs, ok := readBuildSetting("vcs")
if ok {
vcs = " " + vcs
}
dirtySuffix := ""
if dirty == "true" {
dirtySuffix = " (dirty)"
}
return fmt.Sprintf("%s built from%s commit %s%s", banner, vcs, commit, dirtySuffix)
}
if dirty == "true" {
return fmt.Sprintf("%s %s", banner, version)
}
return fmt.Sprintf("%s %s-%s", banner, version, commit)
}
func longVersion() string {
fallback := version
if fallback == "" {
fallback = "(unknown version)"
}
banner := banner
if version != "" {
banner = fmt.Sprintf("%s %s", banner, version)
}
readBuildSetting := buildSettingReader()
version = "built"
cgo, ok := readBuildSetting("CGO_ENABLED")
if ok {
if cgo == "1" {
version += " with CGO"
} else {
version += " without CGO"
}
}
vcs, ok := readBuildSetting("vcs")
if !ok {
return fmt.Sprintf("%s %s", banner, fallback)
}
version = fmt.Sprintf("%s from %s", version, vcs)
commit, ok := readBuildSetting("vcs.revision")
if !ok {
return fmt.Sprintf("%s %s", banner, fallback)
}
version = fmt.Sprintf("%s commit %s", version, commit)
timeStamp, ok := readBuildSetting("vcs.time")
if ok {
t, err := time.Parse(time.RFC3339, timeStamp)
if err == nil {
version = fmt.Sprintf("%s#%v", version, t.Format("2006-01-02"))
}
}
dirty, ok := readBuildSetting("vcs.modified")
if ok && dirty == "true" {
version = fmt.Sprintf("%s (dirty)", version)
}
return fmt.Sprintf("%s %s", banner, version)
}