Skip to content

Commit c6e8431

Browse files
committed
Add more information to icingadb --version ouput
This commit adds the build information (Go version, Git commit) and system information (distribution with version) to the output of `icingadb --version`. When exporting the source code using `git archive`, Git will insert the version information using the export-subst attribute (adds both a pretty version string like `git describe` and the commit hash). `go build` also adds some Git commit information to the resulting binary when building from a Git working directory (only the commit hash and a flag if there are uncommitted changes, but no pretty version string). Note that `go build` does not include Git version information in the binary, so when running directly from a Git working directory, commit information won't be available in the binary, but when doing this, you should be well aware which version you're running anyways. System information is obtained from the `os-release` file.
1 parent 88736f7 commit c6e8431

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Include version information on `git archive'
2+
/internal/version.go export-subst

internal/command/command.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func New() *Command {
3131
}
3232

3333
if flags.Version {
34-
fmt.Println("Icinga DB version:", internal.Version)
34+
internal.Version.Print()
3535
os.Exit(0)
3636
}
3737

internal/version.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
package internal
22

3-
var Version = "v1.0.0-rc2-dev"
3+
import (
4+
"github.com/icinga/icingadb/pkg/version"
5+
)
6+
7+
// Version contains version and Git commit information.
8+
//
9+
// The placeholders are replaced on `git archive` using the `export-subst` attribute.
10+
var Version = version.Version("1.0.0-rc2", "$Format:%(describe)$", "$Format:%H$")

pkg/version/version.go

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package version
2+
3+
import (
4+
"bufio"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"runtime"
9+
"runtime/debug"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
type VersionInfo struct {
15+
Version string
16+
Commit string
17+
}
18+
19+
// Version determines version and commit information based on multiple data sources:
20+
// - Version information dynamically added by `git archive` in the remaining to parameters.
21+
// - A hardcoded version number passed as first parameter.
22+
// - Commit information added to the binary by `go build`.
23+
//
24+
// It's supposed to be called like this in combination with setting the `export-subst` attribute for the corresponding
25+
// file in .gitattributes:
26+
//
27+
// var Version = version.Version("1.0.0-rc2", "$Format:%(describe)$", "$Format:%H$")
28+
//
29+
// When exported using `git archive`, the placeholders are replaced in the file and this version information is
30+
// preferred. Otherwise the hardcoded version is used and augmented with commit information from the build metadata.
31+
func Version(version, gitDescribe, gitHash string) *VersionInfo {
32+
if !strings.HasPrefix(gitDescribe, "$") && !strings.HasPrefix(gitHash, "$") {
33+
return &VersionInfo{
34+
Version: gitDescribe,
35+
Commit: gitHash,
36+
}
37+
} else {
38+
commit := ""
39+
40+
if info, ok := debug.ReadBuildInfo(); ok {
41+
modified := false
42+
43+
for _, setting := range info.Settings {
44+
switch setting.Key {
45+
case "vcs.revision":
46+
commit = setting.Value
47+
case "vcs.modified":
48+
modified, _ = strconv.ParseBool(setting.Value)
49+
}
50+
}
51+
52+
const hashLen = 7 // Same truncation length for the commit hash as used by git describe.
53+
54+
if len(commit) >= hashLen {
55+
version += "-g" + commit[:hashLen]
56+
57+
if modified {
58+
version += "-dirty"
59+
commit += " (modified)"
60+
}
61+
}
62+
}
63+
64+
return &VersionInfo{
65+
Version: version,
66+
Commit: commit,
67+
}
68+
}
69+
}
70+
71+
// Print writes verbose version output to stdout.
72+
func (v *VersionInfo) Print() {
73+
fmt.Println("Icinga DB version:", v.Version)
74+
fmt.Println()
75+
76+
fmt.Println("Build information:")
77+
fmt.Printf(" Go version: %s (%s, %s)\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
78+
if v.Commit != "" {
79+
fmt.Println(" Git commit:", v.Commit)
80+
}
81+
82+
if r, err := readOsRelease(); err == nil {
83+
fmt.Println()
84+
fmt.Println("System information:")
85+
fmt.Println(" Platform:", r.Name)
86+
fmt.Println(" Platform version:", r.DisplayVersion())
87+
}
88+
}
89+
90+
// osRelease contains the information obtained from the os-release file.
91+
type osRelease struct {
92+
Name string
93+
Version string
94+
VersionId string
95+
BuildId string
96+
}
97+
98+
// DisplayVersion returns the most suitable version information for display purposes.
99+
func (o *osRelease) DisplayVersion() string {
100+
if o.Version != "" {
101+
// Most distributions set VERSION
102+
return o.Version
103+
} else if o.VersionId != "" {
104+
// Some only set VERSION_ID (Alpine Linux for example)
105+
return o.VersionId
106+
} else if o.BuildId != "" {
107+
// Others only set BUILD_ID (Arch Linux for example)
108+
return o.BuildId
109+
} else {
110+
return "(unknown)"
111+
}
112+
}
113+
114+
// readOsRelease reads and parses the os-release file.
115+
func readOsRelease() (*osRelease, error) {
116+
for _, path := range []string{"/etc/os-release", "/usr/lib/os-release"} {
117+
f, err := os.Open(path)
118+
if err != nil {
119+
if os.IsNotExist(err) {
120+
continue // Try next path.
121+
} else {
122+
return nil, err
123+
}
124+
}
125+
126+
o := &osRelease{
127+
Name: "Linux", // Suggested default as per os-release(5) man page.
128+
}
129+
130+
scanner := bufio.NewScanner(f)
131+
for scanner.Scan() {
132+
line := scanner.Text()
133+
if strings.HasPrefix(line, "#") {
134+
continue // Ignore comment.
135+
}
136+
137+
parts := strings.SplitN(line, "=", 2)
138+
if len(parts) != 2 {
139+
continue // Ignore empty or possibly malformed line.
140+
}
141+
142+
key := parts[0]
143+
val := parts[1]
144+
145+
// Unquote strings. This isn't fully compliant with the specification which allows using some shell escape
146+
// sequences. However, typically quotes are only used to allow whitespace within the value.
147+
if len(val) >= 2 && (val[0] == '"' || val[0] == '\'') && val[0] == val[len(val)-1] {
148+
val = val[1 : len(val)-1]
149+
}
150+
151+
switch key {
152+
case "NAME":
153+
o.Name = val
154+
case "VERSION":
155+
o.Version = val
156+
case "VERSION_ID":
157+
o.VersionId = val
158+
case "BUILD_ID":
159+
o.BuildId = val
160+
}
161+
}
162+
if err := scanner.Err(); err != nil {
163+
return nil, err
164+
}
165+
166+
return o, nil
167+
}
168+
169+
return nil, errors.New("os-release file not found")
170+
}

0 commit comments

Comments
 (0)