-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
139 lines (128 loc) · 3.78 KB
/
version.go
File metadata and controls
139 lines (128 loc) · 3.78 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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xver/blob/main/LICENSE
package xver
import (
"os"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
"sync"
)
// Info holds basic build information about an application.
//
// Some information may not be available, by default.
//
// To set the app name / app version / build date you can use
// `-X` option when running `go build` cmd.
type Info struct {
// App holds app info.
App App `json:"app"`
// Build holds build info.
Build Build `json:"build"`
}
// App holds basic application information, like name and version.
type App struct {
// Name holds the application name.
// Defaults to executable name.
//
// You can set it during build time with -X 'github.com/actforgood/xver.name=<appName>'.
Name string `json:"name"`
// Version holds the application version.
// Defaults to [debug.BuildInfo.Main.Version].
//
// You can set it during build time with -X 'github.com/actforgood/xver.version=<appVersion>'.
//
// Note from go1.24 release:
// The go build command now sets the main module’s version in the compiled binary based on
// the version control system tag and/or commit. A +dirty suffix will be appended if there
// are uncommitted changes. Use the -buildvcs=false flag to omit version control information
// from the binary.
Version string `json:"version"`
}
// Build holds basic build information,
// like the go version used and application built time.
type Build struct {
// Go holds the go version application was compiled with.
//
// It is taken from [debug.ReadBuildInfo], if available.
Go string `json:"go"`
// Arch is the architecture binary was build for.
//
// It is taken from [debug.ReadBuildInfo], if available.
Arch string `json:"arch"`
// OS is the operating system binary was build for.
//
// It is taken from [debug.ReadBuildInfo], if available.
OS string `json:"os"`
// Commit is the commit sha.
//
// It is taken from [debug.ReadBuildInfo], if available.
//
// You can set it during build time with -X 'github.com/actforgood/xver.commit=<gitCommit>'.
Commit string `json:"commit,omitempty"`
// Date is application build date.
//
// It is taken from [debug.ReadBuildInfo], if available.
//
// You can set it during build time with -X 'github.com/actforgood/xver.date=<buildDate>'.
Date string `json:"date"`
}
var (
once sync.Once
info Info
name = ""
version = ""
date = ""
commit = ""
)
// Information returns information about the application.
func Information() Info {
once.Do(func() {
if name != "" {
info.App.Name = name
} else {
execPath, _ := os.Executable()
info.App.Name = filepath.Base(execPath)
}
info.App.Version = version
info.Build.Date = date
info.Build.Commit = commit
buildInfo, available := debug.ReadBuildInfo()
if !available {
return
}
info.Build.Go = strings.TrimPrefix(buildInfo.GoVersion, "go")
if buildInfo.Main.Version != "" {
regex := regexp.MustCompile(`^v[\d]+\.[\d]+\.[\d]+.*`)
if regex.MatchString(buildInfo.Main.Version) { // remove "v" prefix
info.App.Version = buildInfo.Main.Version[1:]
} else {
info.App.Version = buildInfo.Main.Version
}
}
for _, setting := range buildInfo.Settings {
switch setting.Key {
case "vcs.time":
if info.Build.Date == "" {
info.Build.Date = setting.Value
}
case "vcs.revision":
if info.Build.Commit == "" {
info.Build.Commit = setting.Value
}
case "vcs.modified":
if setting.Value == "true" && !strings.HasSuffix(info.App.Version, "+dirty") {
info.App.Version += "+dirty"
}
case "GOOS":
info.Build.OS = setting.Value
case "GOARCH":
info.Build.Arch = setting.Value
}
}
})
return info
}