-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
183 lines (150 loc) · 4.02 KB
/
version.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
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
package king
import (
"bytes"
"errors"
"fmt"
"runtime"
"strings"
"text/template"
"time"
"github.com/alecthomas/kong"
)
const (
versionKey = "king_version"
buildInfoKey = "king_build_info"
versionInfoTmpl = `
{{.Program}}, version {{.Version}} (revision: {{.Revision}})
build date: {{.Date}}
go version: {{.GoVersion}}
`
)
// VersionFlag displays the version information stored in "version" key form kong.Vars.
//
// Use this flag to show version information.
type VersionFlag bool
// BeforeApply is the actual version command.
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Fprintln(app.Stdout, vars[versionKey])
app.Exit(0)
return nil
}
// BuildInfo represents build information.
type BuildInfo struct {
version string
revision string
goVersion string
location *time.Location
date time.Time
}
// NewBuildInfo creates BuildInformation from version, revision and date. These
// values are typically set with ldflags (via goreleaser for example).
//
// The date has to be in time.RFC3339 format and the revision must be
// at least 8 chars long.
func NewBuildInfo(version string, opts ...Option) (*BuildInfo, error) {
b := BuildInfo{
version: version,
location: time.UTC,
goVersion: runtime.Version(),
date: time.Date(1977, time.May, 25, 0, 0, 0, 0, time.UTC),
}
for _, opt := range opts {
if err := opt(&b); err != nil {
return nil, err
}
}
return &b, nil
}
// Option is a BuildInfo functional option.
type Option func(*BuildInfo) error
// WithRevision sets the git commit revision.
func WithRevision(r string) Option {
return func(b *BuildInfo) error {
if len(r) < 8 {
return errors.New("build revision must be at least 8 chars long")
}
b.revision = r[:8]
return nil
}
}
// WithDateString sets the build date (RFC3339).
func WithDateString(date string) Option {
return func(b *BuildInfo) error {
d, err := time.Parse(time.RFC3339, date)
if err != nil {
return err
}
b.date = d
return nil
}
}
// WithDate sets the build date.
func WithDate(date time.Time) Option {
return func(b *BuildInfo) error {
b.date = date
return nil
}
}
// WithLocation sets the timezone for the build date.
func WithLocation(loc string) Option {
return func(b *BuildInfo) error {
l, err := time.LoadLocation(loc)
if err != nil {
return err
}
b.location = l
return nil
}
}
// Version returns the version information.
func (b *BuildInfo) Version(program string) Version {
return Version{
Version: b.version,
Revision: b.revision,
Date: b.date.In(b.location).Format(time.RFC3339),
GoVersion: b.goVersion,
Program: program,
}
}
// Version represents the version of a go program.
type Version struct {
Version string `json:"version" yaml:"version"`
Revision string `json:"revision" yaml:"revision"`
Date string `json:"date" yaml:"date"`
GoVersion string `json:"go_version" yaml:"go_version"`
Program string `json:"program" yaml:"program"`
}
func (v Version) String() string {
t := template.Must(template.New("version").Parse(versionInfoTmpl))
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "version", v); err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
func (b *BuildInfo) asMap(prefix string) map[string]string {
m := map[string]string{
prefix + "buildinfo-version": b.version,
prefix + "buildinfo-date": b.date.In(b.location).Format(time.RFC3339),
prefix + "buildinfo-go": b.goVersion,
prefix + "buildinfo-revision": b.revision,
}
if prefix != "" {
m[prefix+"buildinfo-location"] = b.location.String()
}
return m
}
func newBuildInfo(prefix string, m map[string]string) *BuildInfo {
if _, ok := m[prefix+"buildinfo-version"]; !ok {
return nil
}
d, _ := time.Parse(time.RFC3339, m[prefix+"buildinfo-date"])
l, _ := time.LoadLocation(m[prefix+"buildinfo-location"])
return &BuildInfo{
version: m[prefix+"buildinfo-version"],
revision: m[prefix+"buildinfo-revision"],
goVersion: m[prefix+"buildinfo-go"],
date: d,
location: l,
}
}