-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildinfo.go
146 lines (122 loc) · 2.88 KB
/
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package buildinfo
import (
"encoding/json"
"fmt"
"runtime"
)
const (
infoConcat = "; "
// infoFmt contains the format used by BuildInfo.Print
infoFmt = `%s, version %s (branch: %s, revision: %s)
build user: %s
build host: %s
build date: %s
go version: %s
platform: %s`
)
var (
GoVersion = runtime.Version()
GoOS = runtime.GOOS
GoArch = runtime.GOARCH
)
// BuildInfo is a container for version- and environment information
type BuildInfo struct {
*VersionInfo
*EnvironmentInfo
}
// NewBuildInfo returns a BuildInfo instance using the provided values
func NewBuildInfo(v *VersionInfo, e *EnvironmentInfo) *BuildInfo {
return &BuildInfo{
VersionInfo: v,
EnvironmentInfo: e,
}
}
// New returns a BuildInfo instance with default values
func New() *BuildInfo {
return NewBuildInfo(NewVersionInfo(), NewEnvironmentInfo())
}
// Parse unmarshals the given JSON byte data into
// a BuildInfo instance. An empty input or even nil
// are considered valid values, and return a default
// instance.
func Parse(info []byte) (result *BuildInfo, err error) {
result = New()
if info == nil || len(info) == 0 {
return
}
err = json.Unmarshal(info, result)
return
}
// TryParse calls Parse and returns a
// default instance in case of an error
func TryParse(info []byte) *BuildInfo {
result, err := Parse(info)
if err != nil {
return New()
}
return result
}
// MustParse calls Parse and panics on any error
func MustParse(info []byte) *BuildInfo {
result, err := Parse(info)
if err != nil {
panic(err)
}
return result
}
// String returns the Version- and Environment information concatenated
func (i *BuildInfo) String() string {
return i.VersionInfo.String() + infoConcat + i.EnvironmentInfo.String()
}
// Clone creates an independant copy of itself.
func (i *BuildInfo) Clone() *BuildInfo {
var (
i2 *BuildInfo
v2 *VersionInfo
e2 *EnvironmentInfo
)
if i.VersionInfo != nil {
v2 = i.VersionInfo.Clone()
}
if i.EnvironmentInfo != nil {
e2 = i.EnvironmentInfo.Clone()
}
i2 = &BuildInfo{
VersionInfo: v2,
EnvironmentInfo: e2,
}
return i2
}
// Equal compares the fields of this instance to the given one
func (i *BuildInfo) Equal(o *BuildInfo) bool {
if i == nil || o == nil {
return i == nil && o == nil
}
return i.VersionInfo.Equal(o.VersionInfo) && i.EnvironmentInfo.Equal(o.EnvironmentInfo)
}
// JSON is a wrapper for json.Marshal using the instance as parameter
func (i *BuildInfo) JSON() ([]byte, error) {
return json.Marshal(i)
}
// Print returns version- and environment information.
func (i *BuildInfo) Print(program string) string {
v := i.VersionInfo
if v == nil {
v = NewVersionInfo()
}
e := i.EnvironmentInfo
if e == nil {
e = NewEnvironmentInfo()
}
return fmt.Sprintf(infoFmt,
program,
v.Version,
v.Branch,
v.ShortRevision(),
e.User,
e.Host,
e.Date,
GoVersion,
GoOS+"/"+GoArch,
)
}