-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildinfo.go
More file actions
59 lines (52 loc) · 1.13 KB
/
Copy pathbuildinfo.go
File metadata and controls
59 lines (52 loc) · 1.13 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
)
// If built by goreleaser, these variables are set by goreleaser using -ldflags.
var (
version = "dev"
commit = "unknown"
date = ""
builtBy = ""
)
func safeSubstr(str string, length int) string {
if len(str) >= length {
return str[:length]
} else {
return str
}
}
func readBuildInfo() (string, error) {
execPath, err := os.Executable()
if err != nil {
return "", err
}
programName := filepath.Base(execPath)
if builtBy == "goreleaser" {
commit = safeSubstr(commit, 8)
date = safeSubstr(date, 10)
} else {
info, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("ReadBuildInfo failed")
}
vcsModified := false
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = safeSubstr(setting.Value, 8)
case "vcs.time":
date = safeSubstr(setting.Value, 10)
case "vcs.modified":
vcsModified = setting.Value == "true"
}
}
if vcsModified {
commit = "locally_modified"
}
}
return fmt.Sprintf("%s version=%s date=%s commit=%s", programName, version, date, commit), nil
}