-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (56 loc) · 1.51 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package build
import (
"fmt"
"runtime/debug"
"strings"
)
// VersionInformation contains everything we know about our appliation.
//
// Actually, that's a lie, it does not contain everything (yet), but contains
// a lot of information.
type VersionInformation struct {
AppName string
Version string
Time string
Revision string
Dirty bool
}
func (info VersionInformation) String() string {
parts := []string{}
if info.AppName != "" {
parts = append(parts, info.AppName)
}
if info.Version != "" {
parts = append(parts, info.Version)
} else if info.Revision != "" {
parts = append(parts, info.Revision)
}
if info.Dirty {
parts = append(parts, "*dirty*")
}
if info.Time != "" {
parts = append(parts, fmt.Sprintf("(%s)", info.Time))
}
return strings.Join(parts, " ")
}
// ReadVersion reads build information and tried to create a VersionInformation
// out of it.
func ReadVersion(appName string) (VersionInformation, error) {
version := VersionInformation{AppName: appName}
info, ok := debug.ReadBuildInfo()
if !ok {
return version, BuildInfoError{}
}
for _, setting := range info.Settings {
if setting.Key == "-ldflags" && strings.HasPrefix(setting.Value, "-buildid=") {
version.Version = strings.TrimPrefix(setting.Value, "-buildid=")
} else if setting.Key == "vcs.revision" {
version.Revision = setting.Value
} else if setting.Key == "vcs.time" {
version.Time = setting.Value
} else if setting.Key == "vcs.modified" {
version.Dirty = true
}
}
return version, nil
}