-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinfo.go
More file actions
232 lines (207 loc) · 6.98 KB
/
info.go
File metadata and controls
232 lines (207 loc) · 6.98 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
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package build
import (
"bytes"
_ "embed"
"fmt"
"runtime"
"strings"
"text/tabwriter"
"time"
"github.com/cockroachdb/cockroachdb-parser/pkg/util/buildutil"
"github.com/cockroachdb/cockroachdb-parser/pkg/util/envutil"
"github.com/cockroachdb/redact"
"github.com/cockroachdb/version"
)
// TimeFormat is the reference format for build.Time. Make sure it stays in sync
// with the string passed to the linker in the root Makefile.
const TimeFormat = "2006/01/02 15:04:05"
// These variables are initialized by Bazel via the linker -X flag
// when compiling release binaries.
var (
utcTime string // Build time in UTC (year/month/day hour:min:sec)
rev string // SHA-1 of this build (git rev-parse)
buildTagOverride string
cgoTargetTriple string
typ string // Type of this build: <empty>, "development", or "release"
channel string
)
// Distribution is changed by the CCL init-time hook in non-APL builds.
var Distribution = "OSS"
var (
cgoCompiler = cgoVersion()
platform = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)
envChannel = envutil.EnvOrDefaultString("COCKROACH_CHANNEL", "unknown")
enabledAssertions = buildutil.CrdbTestBuild
)
var (
//go:embed version.txt
versionTxt string
parsedVersionTxt *version.Version = parseCockroachVersion(versionTxt)
binaryVersion string = computeBinaryVersion(buildTagOverride, parsedVersionTxt, rev)
// binaryVersionTestingOverride is modified by TestingOverrideVersion.
binaryVersionTestingOverride string
)
const (
DefaultTelemetryChannel = "official-binary"
FIPSTelemetryChannel = "official-fips-binary"
)
// IsRelease returns true if the binary was produced by a "release" build.
func IsRelease() bool {
return typ == "release"
}
// SeemsOfficial reports whether this binary is likely to have come from an
// official release channel.
func SeemsOfficial() bool {
return channel == DefaultTelemetryChannel || channel == FIPSTelemetryChannel
}
func parseCockroachVersion(versionTxt string) *version.Version {
txt := strings.TrimSpace(versionTxt)
v, err := version.Parse(txt)
if err != nil {
panic(fmt.Errorf("could not parse version.txt: %w", err))
}
return &v
}
func computeBinaryVersion(
buildTagOverride string, parsedVersionTxt *version.Version, revision string,
) string {
if buildTagOverride != "" {
return buildTagOverride
}
if IsRelease() {
return parsedVersionTxt.String()
}
if revision != "" {
return fmt.Sprintf("%s-dev-%s", parsedVersionTxt.String(), revision)
}
return fmt.Sprintf("%s-dev", parsedVersionTxt.String())
}
// BinaryVersion returns the version prefix, patch number and metadata of the
// current build.
func BinaryVersion() string {
if binaryVersionTestingOverride != "" {
return binaryVersionTestingOverride
}
return binaryVersion
}
// VersionForURLs is used to determine the version to use in public-facing doc URLs.
// It returns "vX.Y" for all release versions, and all prerelease versions >= "alpha.1".
// X and Y are the major and minor, respectively, of the version specified in version.txt.
// For all other prerelease versions, it returns "dev".
// N.B. new public-facing doc URLs are expected to be up beginning with the "alpha.1" prerelease. Otherwise, "dev" will
// cause the url mapper to redirect to the latest stable release.
func VersionForURLs() string {
if parsedVersionTxt.IsPrerelease() {
phaseAndOrdinal := parsedVersionTxt.Format("%P.%o")
// builds prior to "alpha.1" use 'dev' in their URLs
if phaseAndOrdinal < "alpha.1" {
return "dev"
}
} else if parsedVersionTxt.IsCustomOrNightlyBuild() {
return "dev"
}
return parsedVersionTxt.Major().String()
}
// BranchReleaseSeries returns tha major and minor in version.txt, without
// allowing for any overrides.
func BranchReleaseSeries() (year, ordinal int) {
major := parsedVersionTxt.Major()
return major.Year, major.Ordinal
}
func init() {
// Allow tests to override the version.txt contents.
if versionOverride := envutil.EnvOrDefaultString(
"COCKROACH_TESTING_VERSION_OVERRIDE", ""); versionOverride != "" {
TestingOverrideVersion(versionOverride)
}
}
// Short returns a pretty printed build and version summary.
func (b Info) Short() redact.RedactableString {
plat := b.Platform
if b.CgoTargetTriple != "" {
plat = b.CgoTargetTriple
}
return redact.Sprintf("CockroachDB %s %s (%s, built %s, %s)",
redact.SafeString(b.Distribution),
redact.SafeString(b.Tag),
redact.SafeString(plat),
redact.SafeString(b.Time),
redact.SafeString(b.GoVersion),
)
}
// Long returns a pretty printed build summary
func (b Info) Long() string {
var buf bytes.Buffer
tw := tabwriter.NewWriter(&buf, 2, 1, 2, ' ', 0)
fmt.Fprintf(tw, "Build Tag: %s\n", b.Tag)
fmt.Fprintf(tw, "Build Time: %s\n", b.Time)
fmt.Fprintf(tw, "Distribution: %s\n", b.Distribution)
fmt.Fprintf(tw, "Platform: %s", b.Platform)
if b.CgoTargetTriple != "" {
fmt.Fprintf(tw, " (%s)", b.CgoTargetTriple)
}
fmt.Fprintln(tw)
fmt.Fprintf(tw, "Go Version: %s\n", b.GoVersion)
fmt.Fprintf(tw, "C Compiler: %s\n", b.CgoCompiler)
fmt.Fprintf(tw, "Build Commit ID: %s\n", b.Revision)
fmt.Fprintf(tw, "Build Type: %s\n", b.Type)
fmt.Fprintf(tw, "Enabled Assertions: %t", b.EnabledAssertions) // No final newline: cobra prints one for us.
_ = tw.Flush()
return buf.String()
}
// GoTime parses the utcTime string and returns a time.Time.
func (b Info) GoTime() time.Time {
val, err := time.Parse(TimeFormat, b.Time)
if err != nil {
return time.Time{}
}
return val
}
// Timestamp parses the utcTime string and returns the number of seconds since epoch.
func (b Info) Timestamp() (int64, error) {
val, err := time.Parse(TimeFormat, b.Time)
if err != nil {
return 0, err
}
return val.Unix(), nil
}
// GetInfo returns an Info struct populated with the build information.
func GetInfo() Info {
ch := channel
if ch == "" {
ch = "unknown"
}
return Info{
GoVersion: runtime.Version(),
Tag: BinaryVersion(),
Time: utcTime,
Revision: rev,
CgoCompiler: cgoCompiler,
CgoTargetTriple: cgoTargetTriple,
Platform: platform,
Distribution: Distribution,
Type: typ,
Channel: ch,
EnvChannel: envChannel,
EnabledAssertions: enabledAssertions,
}
}
// TestingOverrideVersion allows tests to override the binary version
// reported by cockroach.
func TestingOverrideVersion(v string) func() {
prevOverride := binaryVersionTestingOverride
binaryVersionTestingOverride = v
return func() { binaryVersionTestingOverride = prevOverride }
}
// MakeIssueURL produces a URL to a CockroachDB issue.
func MakeIssueURL(issue int) string {
return fmt.Sprintf("https://go.crdb.dev/issue-v/%d/%s", issue, VersionForURLs())
}
// ParsedVersion returns the parsed version.txt.
func ParsedVersion() *version.Version {
return parsedVersionTxt
}