-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
67 lines (56 loc) · 1.44 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
// Copyright ©2022 Evolution. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Application version string related functionality.
//
// Implementation should work for case when application binary is built via "go build" and
// version injection via "ldflags" as well as when binary is installed via "go install" in
// which case debug.BuildInfo is used to pull relevant version information.
package main
import (
"fmt"
"os"
"runtime/debug"
"time"
)
// Value injected during build with -ldflags="-X main.version={ver}".
var (
version string
vInfo versionInfo
)
func init() {
// A case when version is passed in via -ldflags="-X main.version=xxx"
if version != "" {
vInfo.version = version
}
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
if vInfo.version == "" {
vInfo.version = bi.Main.Version
}
for _, v := range bi.Settings {
switch v.Key {
case "vcs.revision":
vInfo.revision = v.Value
case "vcs.time":
vInfo.time, _ = time.Parse(time.RFC3339, v.Value)
}
}
}
// versionInfo is struct that includes relevant version information.
type versionInfo struct {
time time.Time
version string
revision string
}
func (v versionInfo) String() string {
if v.revision == "" {
return v.version
}
return fmt.Sprintf("%s %s (%s)", v.version, v.revision, v.time.Format("2006-01-02"))
}
func printVersion() {
fmt.Fprintln(os.Stderr, vInfo)
}