forked from kenstir/hemlock-sendmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildinfo.go
47 lines (42 loc) · 963 Bytes
/
buildinfo.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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
)
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)
info, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("ReadBuildInfo failed")
}
vcs_modified := ""
vcs_rev := ""
vcs_time := ""
for _, element := range info.Settings {
if element.Key == "vcs.revision" {
vcs_rev = safeSubstr(element.Value, 8)
} else if element.Key == "vcs.time" {
vcs_time = safeSubstr(element.Value, 10)
} else if element.Key == "vcs.modified" {
if element.Value == "true" {
vcs_modified = "(LOCALLY MODIFIED)"
} else {
vcs_modified = ""
}
}
}
return fmt.Sprintf("%s date=%s commit=%s %s", programName, vcs_time, vcs_rev, vcs_modified), nil
}