Skip to content

Commit cafd71c

Browse files
authored
Emit version in JSON format for --json-version (#3129)
1 parent d0c2094 commit cafd71c

File tree

5 files changed

+72
-25
lines changed

5 files changed

+72
-25
lines changed

config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func deprecateFlags(fs *pflag.FlagSet) error {
6969
func addProcessFlags(fs *pflag.FlagSet) {
7070
// If true, print the version and quit.
7171
fs.Bool(VersionKey, false, "If true, print version and quit")
72+
fs.Bool(VersionJSONKey, false, "If true, print version in JSON format and quit")
7273
}
7374

7475
func addNodeFlags(fs *pflag.FlagSet) {

config/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
ConfigContentKey = "config-file-content"
1414
ConfigContentTypeKey = "config-file-content-type"
1515
VersionKey = "version"
16+
VersionJSONKey = "version-json"
1617
GenesisFileKey = "genesis-file"
1718
GenesisFileContentKey = "genesis-file-content"
1819
NetworkNameKey = "network-id"

main/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"os"
@@ -29,8 +30,24 @@ func main() {
2930
os.Exit(1)
3031
}
3132

33+
if v.GetBool(config.VersionJSONKey) && v.GetBool(config.VersionKey) {
34+
fmt.Println("can't print both JSON and human readable versions")
35+
os.Exit(1)
36+
}
37+
38+
if v.GetBool(config.VersionJSONKey) {
39+
versions := version.GetVersions()
40+
jsonBytes, err := json.MarshalIndent(versions, "", " ")
41+
if err != nil {
42+
fmt.Printf("couldn't marshal versions: %s\n", err)
43+
os.Exit(1)
44+
}
45+
fmt.Println(string(jsonBytes))
46+
os.Exit(0)
47+
}
48+
3249
if v.GetBool(config.VersionKey) {
33-
fmt.Print(version.String)
50+
fmt.Println(version.GetVersions().String())
3451
os.Exit(0)
3552
}
3653

version/string.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@ import (
99
"strings"
1010
)
1111

12-
var (
13-
// String is displayed when CLI arg --version is used
14-
String string
12+
// GitCommit is set in the build script at compile time
13+
var GitCommit string
1514

16-
// GitCommit is set in the build script at compile time
17-
GitCommit string
18-
)
15+
// Versions contains the versions relevant to a build of avalanchego. In
16+
// addition to supporting construction of the string displayed by
17+
// --version, it is used to produce the output of --version-json and can
18+
// be used to unmarshal that output.
19+
type Versions struct {
20+
Application string `json:"application"`
21+
Database string `json:"database"`
22+
RPCChainVM uint64 `json:"rpcchainvm"`
23+
// Commit may be empty if GitCommit was not set at compile time
24+
Commit string `json:"commit"`
25+
Go string `json:"go"`
26+
}
1927

20-
func init() {
21-
format := "%s [database=%s, rpcchainvm=%d"
22-
args := []interface{}{
23-
CurrentApp,
24-
CurrentDatabase,
25-
RPCChainVMProtocol,
26-
}
27-
if GitCommit != "" {
28-
format += ", commit=%s"
29-
args = append(args, GitCommit)
28+
func GetVersions() *Versions {
29+
return &Versions{
30+
Application: CurrentApp.String(),
31+
Database: CurrentDatabase.String(),
32+
RPCChainVM: uint64(RPCChainVMProtocol),
33+
Commit: GitCommit,
34+
Go: strings.TrimPrefix(runtime.Version(), "go"),
3035
}
36+
}
3137

32-
// add golang version
33-
goVersion := runtime.Version()
34-
goVersionNumber := strings.TrimPrefix(goVersion, "go")
35-
format += ", go=%s"
36-
args = append(args, goVersionNumber)
37-
38-
format += "]\n"
39-
String = fmt.Sprintf(format, args...)
38+
func (v *Versions) String() string {
39+
// This format maintains consistency with previous --version output
40+
versionString := fmt.Sprintf("%s [database=%s, rpcchainvm=%d, ", v.Application, v.Database, v.RPCChainVM)
41+
if len(v.Commit) > 0 {
42+
versionString += fmt.Sprintf("commit=%s, ", v.Commit)
43+
}
44+
return versionString + fmt.Sprintf("go=%s]", v.Go)
4045
}

version/string_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package version
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestVersionsGetString(t *testing.T) {
13+
versions := Versions{
14+
Application: "1",
15+
Database: "2",
16+
RPCChainVM: 3,
17+
Commit: "4",
18+
Go: "5",
19+
}
20+
require.Equal(t, "1 [database=2, rpcchainvm=3, commit=4, go=5]", versions.String())
21+
versions.Commit = ""
22+
require.Equal(t, "1 [database=2, rpcchainvm=3, go=5]", versions.String())
23+
}

0 commit comments

Comments
 (0)