Skip to content

Commit 61ef976

Browse files
committed
fixup: Respond to review comments
1 parent eff4dc6 commit 61ef976

File tree

3 files changed

+86
-47
lines changed

3 files changed

+86
-47
lines changed

main/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ func main() {
2929
os.Exit(1)
3030
}
3131

32+
if v.GetBool(config.JSONVersionKey) && v.GetBool(config.VersionKey) {
33+
fmt.Printf("can't print both JSON and human readable versions\n")
34+
os.Exit(1)
35+
}
36+
3237
if v.GetBool(config.JSONVersionKey) {
33-
fmt.Print(version.JSONString)
38+
fmt.Println(version.GetVersions().JSON())
3439
os.Exit(0)
3540
}
3641

3742
if v.GetBool(config.VersionKey) {
38-
fmt.Print(version.String)
43+
fmt.Println(version.GetVersions().String())
3944
os.Exit(0)
4045
}
4146

version/string.go

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,47 @@ import (
1111
"strings"
1212
)
1313

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"`
2827
}
2928

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"),
3735
}
38-
39-
// Add git commit if available
4036
if GitCommit != "" {
41-
versions = append(versions, namedVersion{name: "commit", version: GitCommit})
37+
versions.Commit = GitCommit
4238
}
39+
return versions
40+
}
4341

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 {
5443
// 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)
6447
}
48+
return versionString + fmt.Sprintf("go=%s]", v.Go)
49+
}
6550

66-
jsonBytes, err := json.MarshalIndent(versionMap, "", " ")
51+
func (v *Versions) JSON() string {
52+
jsonBytes, err := json.MarshalIndent(v, "", " ")
6753
if err != nil {
6854
panic(err)
6955
}
70-
JSONString = string(jsonBytes) + "\n"
56+
return string(jsonBytes)
7157
}

version/string_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestVersionsGetString(t *testing.T) {
15+
versions := Versions{
16+
Application: "1",
17+
Database: "2",
18+
RPCChainVM: "3",
19+
Go: "5",
20+
}
21+
expectedVersions := fmt.Sprintf(
22+
"%s [database=%s, rpcchainvm=%s, go=%s]",
23+
versions.Application,
24+
versions.Database,
25+
versions.RPCChainVM,
26+
versions.Go,
27+
)
28+
require.Equal(t, expectedVersions, versions.String())
29+
30+
versions.Commit = "eafd"
31+
expectedVersions = fmt.Sprintf(
32+
"%s [database=%s, rpcchainvm=%s, commit=%s, go=%s]",
33+
versions.Application,
34+
versions.Database,
35+
versions.RPCChainVM,
36+
versions.Commit,
37+
versions.Go,
38+
)
39+
require.Equal(t, expectedVersions, versions.String())
40+
}
41+
42+
func TestVersionsJSON(t *testing.T) {
43+
versions := GetVersions()
44+
jsonString := versions.JSON()
45+
unmarshalledVersions := &Versions{}
46+
require.NoError(t, json.Unmarshal([]byte(jsonString), unmarshalledVersions))
47+
require.Equal(t, versions, unmarshalledVersions)
48+
}

0 commit comments

Comments
 (0)