File tree Expand file tree Collapse file tree 2 files changed +41
-9
lines changed Expand file tree Collapse file tree 2 files changed +41
-9
lines changed Original file line number Diff line number Diff line change @@ -5,18 +5,21 @@ import (
5
5
"log"
6
6
"os"
7
7
"path/filepath"
8
+ "runtime/debug"
8
9
"strings"
10
+ "sync"
9
11
10
12
"github.com/abice/go-enum/generator"
11
13
"github.com/labstack/gommon/color"
12
14
"github.com/urfave/cli/v2"
13
15
)
14
16
15
17
var (
16
- version string
17
- commit string
18
- date string
19
- builtBy string
18
+ versionOnce sync.Once
19
+ version string
20
+ commit string
21
+ date string
22
+ builtBy string
20
23
)
21
24
22
25
type rootT struct {
@@ -47,11 +50,39 @@ type rootT struct {
47
50
OutputSuffix string
48
51
}
49
52
53
+ func initializeVersion () {
54
+ versionOnce .Do (func () {
55
+ if version != "" {
56
+ return
57
+ }
58
+ buildInfo , ok := debug .ReadBuildInfo ()
59
+ if ! ok {
60
+ return
61
+ }
62
+ builtBy = "go install"
63
+ version = buildInfo .Main .Version
64
+ for _ , setting := range buildInfo .Settings {
65
+ switch setting .Key {
66
+ case "vcs.revision" :
67
+ commit = setting .Value
68
+ case "vcs.time" :
69
+ date = setting .Value
70
+ case "vcs.modified" :
71
+ if setting .Value == "true" {
72
+ commit += "-modified"
73
+ }
74
+ }
75
+ }
76
+ })
77
+ }
78
+
50
79
func main () {
51
80
var argv rootT
52
81
82
+ initializeVersion ()
83
+
53
84
clr := color .New ()
54
- out := func (format string , args ... interface {} ) {
85
+ out := func (format string , args ... any ) {
55
86
_ , _ = fmt .Fprintf (clr .Output (), format , args ... )
56
87
}
57
88
Original file line number Diff line number Diff line change @@ -725,11 +725,12 @@ func TestCliFlagAliases(t *testing.T) {
725
725
}
726
726
727
727
func TestGlobalVariables (t * testing.T ) {
728
+ initializeVersion ()
728
729
// Test that global variables are properly declared
729
- assert .Equal (t , "" , version ) // Should be empty by default ( set during build)
730
-
707D
assert .Equal (t , "" , commit ) // Should be empty by default (set during build)
731
- assert .Equal (t , "" , date ) // Should be empty by default (set during build)
732
- assert .Equal (t , "" , builtBy ) // Should be empty by default ( set during build)
730
+ assert .Equal (t , "(devel) " , version ) // Default is "(devel)" unless set during build
731
+ assert .Equal (t , "" , commit ) // Should be empty by default (set during build)
732
+ assert .Equal (t , "" , date ) // Should be empty by default (set during build)
733
+ assert .Equal (t , "go install " , builtBy ) // Default is "go install" unless set during build
733
734
}
734
735
735
736
func TestGlobFilenamesEdgeCases (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments