github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/build/info.go (about) 1 // Copyright 2015 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package build 12 13 import ( 14 "fmt" 15 "runtime" 16 "time" 17 18 "github.com/cockroachdb/cockroach/pkg/util/envutil" 19 "github.com/cockroachdb/cockroach/pkg/util/version" 20 ) 21 22 // TimeFormat is the reference format for build.Time. Make sure it stays in sync 23 // with the string passed to the linker in the root Makefile. 24 const TimeFormat = "2006/01/02 15:04:05" 25 26 var ( 27 // These variables are initialized via the linker -X flag in the 28 // top-level Makefile when compiling release binaries. 29 tag = "unknown" // Tag of this build (git describe --tags w/ optional '-dirty' suffix) 30 utcTime string // Build time in UTC (year/month/day hour:min:sec) 31 rev string // SHA-1 of this build (git rev-parse) 32 cgoCompiler = cgoVersion() 33 cgoTargetTriple string 34 platform = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH) 35 // Distribution is changed by the CCL init-time hook in non-APL builds. 36 Distribution = "OSS" 37 typ string // Type of this build: <empty>, "development", or "release" 38 channel = "unknown" 39 envChannel = envutil.EnvOrDefaultString("COCKROACH_CHANNEL", "unknown") 40 ) 41 42 // IsRelease returns true if the binary was produced by a "release" build. 43 func IsRelease() bool { 44 return typ == "release" 45 } 46 47 // SeemsOfficial reports whether this binary is likely to have come from an 48 // official release channel. 49 func SeemsOfficial() bool { 50 return channel == "official-binary" || channel == "source-archive" 51 } 52 53 // VersionPrefix returns the version prefix of the current build. 54 func VersionPrefix() string { 55 v, err := version.Parse(tag) 56 if err != nil { 57 return "dev" 58 } 59 return fmt.Sprintf("v%d.%d", v.Major(), v.Minor()) 60 } 61 62 func init() { 63 // Allow tests to override the tag. 64 if tagOverride := envutil.EnvOrDefaultString( 65 "COCKROACH_TESTING_VERSION_TAG", ""); tagOverride != "" { 66 tag = tagOverride 67 } 68 } 69 70 // Short returns a pretty printed build and version summary. 71 func (b Info) Short() string { 72 plat := b.Platform 73 if b.CgoTargetTriple != "" { 74 plat = b.CgoTargetTriple 75 } 76 return fmt.Sprintf("CockroachDB %s %s (%s, built %s, %s)", 77 b.Distribution, b.Tag, plat, b.Time, b.GoVersion) 78 } 79 80 // GoTime parses the utcTime string and returns a time.Time. 81 func (b Info) GoTime() time.Time { 82 val, err := time.Parse(TimeFormat, b.Time) 83 if err != nil { 84 return time.Time{} 85 } 86 return val 87 } 88 89 // Timestamp parses the utcTime string and returns the number of seconds since epoch. 90 func (b Info) Timestamp() (int64, error) { 91 val, err := time.Parse(TimeFormat, b.Time) 92 if err != nil { 93 return 0, err 94 } 95 return val.Unix(), nil 96 } 97 98 // GetInfo returns an Info struct populated with the build information. 99 func GetInfo() Info { 100 return Info{ 101 GoVersion: runtime.Version(), 102 Tag: tag, 103 Time: utcTime, 104 Revision: rev, 105 CgoCompiler: cgoCompiler, 106 CgoTargetTriple: cgoTargetTriple, 107 Platform: platform, 108 Distribution: Distribution, 109 Type: typ, 110 Channel: channel, 111 EnvChannel: envChannel, 112 } 113 } 114 115 // TestingOverrideTag allows tests to override the build tag. 116 func TestingOverrideTag(t string) func() { 117 prev := tag 118 tag = t 119 return func() { tag = prev } 120 }