This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
version.go
78 lines (71 loc) · 1.78 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
68
69
70
71
72
73
74
75
76
77
78
package mevcommit
import (
"runtime/debug"
"strings"
)
var (
// version will be the version git tag if the binary is built with
// the Makefile and the tag is set. If the tag does not exist the
// version will be set to "(devel)". If the binary is built some
// other way, it will be set to "unknown".
version = "unknown"
// revision is set from the vcs.revision tag in Go 1.18+.
revision = "unknown"
// dirtyBuild is set from the vcs.modified tag in Go 1.18+.
dirtyBuild = true
)
func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, kv := range info.Settings {
if kv.Value == "" {
continue
}
switch kv.Key {
case "vcs.revision":
revision = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}
}
// Version returns the build version string of the binary in format:
//
// <tag>-<commit-hash>[-dirty]
//
// If the tag is not set during build (the tag does not exist or the binary is
// not build with the Makefile), the tag element will remain empty. If the
// binary is built outside the vcs repository, the returned result will be
// "devel". If the binary is built with the Makefile and the repository
// is dirty, the -dirty element will be appended to the result.
//
// Examples:
//
// "devel"
// "rev-333ab74"
// "rev-333ab74-dirty"
// "v1.0.0-rev-333ab74"
// "v1.0.0-rev-333ab74-dirty"
func Version() string {
parts := make([]string, 0, 3)
if version != "unknown" && version != "" {
parts = append(parts, version)
}
if revision != "unknown" && revision != "" {
parts = append(parts, "rev")
commit := revision
if len(commit) > 7 {
commit = commit[:7]
}
parts = append(parts, commit)
if dirtyBuild {
parts = append(parts, "dirty")
}
}
if len(parts) == 0 {
return "devel"
}
return strings.Join(parts, "-")
}