@@ -11,61 +11,47 @@ import (
11
11
"strings"
12
12
)
13
13
14
- var (
15
- // String is displayed when CLI arg --version is used
16
- String string
17
-
18
- // String is displayed when CLI arg --json-version is used
19
- JSONString string
20
-
21
- // GitCommit is set in the build script at compile time
22
- GitCommit string
23
- )
24
-
25
- type namedVersion struct {
26
- name string
27
- version string
14
+ // GitCommit is set in the build script at compile time
15
+ var GitCommit string
16
+
17
+ // Versions contains the versions relevant to a build of avalanchego. In
18
+ // addition to supporting construction of the strings displayed by
19
+ // --version and --json-version, it can be used to unmarshal the output
20
+ // of --json-version.
21
+ type Versions struct {
22
+ Application string `json:"application"`
23
+ Database string `json:"database"`
24
+ RPCChainVM string `json:"rpcchainvm"`
25
+ Commit string `json:"commit"`
26
+ Go string `json:"go"`
28
27
}
29
28
30
- func init () {
31
- // Define the ordered set of versions that are common to regular and JSON
32
- // output. The order is maintained to ensure consistency with previous
33
- // --version output.
34
- versions := []namedVersion {
35
- {name : "database" , version : CurrentDatabase .String ()},
36
- {name : "rpcchainvm" , version : strconv .FormatUint (uint64 (RPCChainVMProtocol ), 10 )},
29
+ func GetVersions () * Versions {
30
+ versions := & Versions {
31
+ Application : CurrentApp .String (),
32
+ Database : CurrentDatabase .String (),
33
+ RPCChainVM : strconv .FormatUint (uint64 (RPCChainVMProtocol ), 10 ),
34
+ Go : strings .TrimPrefix (runtime .Version (), "go" ),
37
35
}
38
-
39
- // Add git commit if available
40
36
if GitCommit != "" {
41
- versions = append ( versions , namedVersion { name : "commit" , version : GitCommit })
37
+ versions . Commit = GitCommit
42
38
}
39
+ return versions
40
+ }
43
41
44
- // Add golang version
45
- goVersion := runtime .Version ()
46
- goVersionNumber := strings .TrimPrefix (goVersion , "go" )
47
- versions = append (versions , namedVersion {name : "go" , version : goVersionNumber })
48
-
49
- // Set the value of the regular version string
50
- versionPairs := make ([]string , len (versions ))
51
- for i , v := range versions {
52
- versionPairs [i ] = fmt .Sprintf ("%s=%s" , v .name , v .version )
53
- }
42
+ func (v * Versions ) String () string {
54
43
// This format maintains consistency with previous --version output
55
- String = fmt .Sprintf ("%s [%s]\n " , CurrentApp , strings .Join (versionPairs , ", " ))
56
-
57
- // Include the app version as a regular value in the JSON output
58
- versions = append (versions , namedVersion {name : "app" , version : CurrentApp .String ()})
59
-
60
- // Convert versions to a map for more compact JSON output
61
- versionMap := make (map [string ]string , len (versions ))
62
- for _ , v := range versions {
63
- versionMap [v .name ] = v .version
44
+ versionString := fmt .Sprintf ("%s [database=%s, rpcchainvm=%s, " , v .Application , v .Database , v .RPCChainVM )
45
+ if len (v .Commit ) > 0 {
46
+ versionString += fmt .Sprintf ("commit=%s, " , v .Commit )
64
47
}
48
+ return versionString + fmt .Sprintf ("go=%s]" , v .Go )
49
+ }
65
50
66
- jsonBytes , err := json .MarshalIndent (versionMap , "" , " " )
51
+ func (v * Versions ) JSON () string {
52
+ jsonBytes , err := json .MarshalIndent (v , "" , " " )
67
53
if err != nil {
68
54
panic (err )
69
55
}
70
- JSONString = string (jsonBytes ) + " \n "
56
+ return string (jsonBytes )
71
57
}
0 commit comments