-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildinfo.go
More file actions
276 lines (240 loc) · 6.95 KB
/
Copy pathbuildinfo.go
File metadata and controls
276 lines (240 loc) · 6.95 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildinfo
import (
"encoding/json"
"io"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/go-pogo/errors"
)
//goland:noinspection GoUnusedConst
const (
TimeFormat = time.RFC3339
// ShortFlag is the default flag to print the current build information.
ShortFlag = "v"
// LongFlag is an alternative long version that may be used together with ShortFlag.
LongFlag = "version"
// MetricName is the default name for the metric (without namespace).
MetricName = "buildinfo"
// MetricHelp is the default help text to describe the metric.
MetricHelp = "Metric with build information labels and a constant value of '1'."
// PathPattern is the default path for an http handler route.
PathPattern = "/version"
// reserved keys
keyVersion = "version"
keyGoVersion = "goversion"
keyRevision = "vcs.revision"
keyTime = "vcs.time"
keyModified = "vcs.modified"
)
func Version(info *debug.BuildInfo) string {
if info == nil {
return ""
}
return info.Main.Version
}
// Revision returns the (short) commit hash the release is build from according
// to the [debug.BuildSetting] with "vcs.revision" key.
func Revision(info *debug.BuildInfo) string {
return Setting(info, keyRevision)
}
func Time(info *debug.BuildInfo) time.Time {
if set := Setting(info, keyTime); set != "" {
t, _ := time.Parse(TimeFormat, set)
return t
}
return time.Time{}
}
// BuildInfo contains the relevant information of the current release's build
// version, revision and time.
type BuildInfo struct {
info *debug.BuildInfo
// GoVersion is the version of the Go toolchain that built the binary
// (for example, "go1.19.2").
GoVersion string
// Version of the release.
Version string
// Revision is the (short) commit hash the release is build from.
Revision string
// Time of the commit the release is build from.
Time time.Time
}
// New creates a new BuildInfo with a set version.
func New(version string) *BuildInfo {
return &BuildInfo{
GoVersion: runtime.Version(),
Version: version,
}
}
const ErrNoBuildInfo = "no build information available"
// Read creates a new [BuildInfo] by reading the debug buildinfo using
// [debug.ReadBuildInfo] and Setting its keys using the read values.
// It returns an [ErrNoBuildInfo] error when reading is not successful.
func Read() (*BuildInfo, error) {
var bld BuildInfo
if !bld.init() {
return nil, errors.New(ErrNoBuildInfo)
}
bld.GoVersion = bld.info.GoVersion
bld.Version = Version(bld.info)
bld.Revision = Revision(bld.info)
bld.Time = Time(bld.info)
return &bld, nil
}
func (bld *BuildInfo) init() bool {
if bld.info != nil {
return bld.info.GoVersion != ""
}
if info, ok := debug.ReadBuildInfo(); ok {
bld.info = info
return true
}
bld.info = new(debug.BuildInfo)
return false
}
// Internal returns the used [debug.BuildInfo], or tries to read it using
// [debug.ReadBuildInfo] when not already done.
func (bld *BuildInfo) Internal() *debug.BuildInfo {
if !bld.init() {
return nil
}
return bld.info
}
// AppName returns the name of the application based on the package path of
// the main package for the binary. For example, a package path of
// "golang.org/x/tools/cmd/stringer" returns "stringer".
func (bld *BuildInfo) AppName() string {
if !bld.init() {
return ""
}
return AppName(bld.info)
}
// AppName returns the name of the application based on the package path of
// the main package for the binary. For example, a package path of
// "golang.org/x/tools/cmd/stringer" returns "stringer".
func AppName(info *debug.BuildInfo) string {
if info == nil || info.Path == "" || info.Path == "/" {
return ""
}
if i := strings.LastIndex(info.Path, "/"); i >= 0 {
return info.Path[i+1:]
}
return info.Path
}
// Module returns the matching [debug.Module] description of a dependency
// module, both direct and indirect, that contributed packages to the build of
// this binary.
func (bld *BuildInfo) Module(name string) *debug.Module {
if !bld.init() {
return nil
}
return Module(bld.info, name)
}
// Module returns the matching [debug.Module] description of a dependency
// module, both direct and indirect, that contributed packages to the build of
// this binary.
func Module(info *debug.BuildInfo, name string) *debug.Module {
if info == nil {
return nil
}
if name == "main" {
return &info.Main
}
for _, mod := range info.Deps {
if mod.Path == name {
return mod
}
}
return nil
}
// Setting returns the value of the matching [debug.BuildSetting] used to build
// the binary.
func (bld *BuildInfo) Setting(key string) string {
if !bld.init() {
return ""
}
return Setting(bld.info, key)
}
// Setting returns the value of the matching [debug.BuildSetting] used to build
// the binary.
func Setting(info *debug.BuildInfo, key string) string {
if info == nil {
return ""
}
for _, set := range info.Settings {
if set.Key == key {
return set.Value
}
}
return ""
}
// Map returns the build information as a map. Field names are lowercase.
// Empty fields are omitted.
func (bld *BuildInfo) Map() map[string]string {
m := make(map[string]string, 5)
m[keyVersion] = bld.Version
if bld.GoVersion != "" {
m[keyGoVersion] = bld.GoVersion
}
if bld.Revision != "" {
m[keyRevision] = bld.Revision
}
if !bld.Time.IsZero() {
m[keyTime] = bld.Time.Format(TimeFormat)
}
return m
}
// String returns the string representation of the build information.
// It always includes the release version. Other fields are omitted when empty.
// Examples:
// - version only: `8.5.0`
// - version and revision `8.5.0 (#fedcba)`
// - version and date: `8.5.0 (2020-06-16 19:53)`
// - all: `8.5.0 (#fedcba @ 2020-06-16 19:53)`
func (bld *BuildInfo) String() string {
if bld.Revision == "" && bld.Time.IsZero() {
return bld.Version
}
var buf strings.Builder
_, _ = buf.WriteString(bld.Version)
if bld.Revision != "" {
_, _ = buf.WriteRune(' ')
_, _ = buf.WriteString(bld.Revision)
}
if !bld.Time.IsZero() {
_, _ = buf.WriteString(" (")
_, _ = buf.WriteString(bld.Time.Format(TimeFormat))
_, _ = buf.WriteString(")")
}
return buf.String()
}
var _ json.Marshaler = (*BuildInfo)(nil)
// MarshalJSON returns valid JSON output.
// Empty fields within buildInfo are omitted.
func (bld *BuildInfo) MarshalJSON() ([]byte, error) {
// WriteString on strings.Builder never returns an error
var buf strings.Builder
bld.writeJson(&buf)
return []byte(buf.String()), nil
}
func (bld *BuildInfo) writeJson(w io.StringWriter) {
_, _ = w.WriteString(`{"version":"`)
_, _ = w.WriteString(bld.Version)
if bld.Revision != "" {
_, _ = w.WriteString(`","revision":"`)
_, _ = w.WriteString(bld.Revision)
}
if !bld.Time.IsZero() {
_, _ = w.WriteString(`","time":"`)
_, _ = w.WriteString(bld.Time.Format(TimeFormat))
}
if bld.GoVersion != "" {
_, _ = w.WriteString(`","goversion":"`)
_, _ = w.WriteString(bld.GoVersion)
}
_, _ = w.WriteString(`"}`)
}