From cafd71c8c3f433faf92c207164e4a1e8548ce1e9 Mon Sep 17 00:00:00 2001 From: marun Date: Fri, 21 Jun 2024 20:26:01 +0200 Subject: [PATCH] Emit version in JSON format for --json-version (#3129) --- config/flags.go | 1 + config/keys.go | 1 + main/main.go | 19 ++++++++++++++- version/string.go | 53 +++++++++++++++++++++++------------------- version/string_test.go | 23 ++++++++++++++++++ 5 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 version/string_test.go diff --git a/config/flags.go b/config/flags.go index 661eb9e84e66..3fb99e5f03e3 100644 --- a/config/flags.go +++ b/config/flags.go @@ -69,6 +69,7 @@ func deprecateFlags(fs *pflag.FlagSet) error { func addProcessFlags(fs *pflag.FlagSet) { // If true, print the version and quit. fs.Bool(VersionKey, false, "If true, print version and quit") + fs.Bool(VersionJSONKey, false, "If true, print version in JSON format and quit") } func addNodeFlags(fs *pflag.FlagSet) { diff --git a/config/keys.go b/config/keys.go index 25348ae54c8d..289c83170614 100644 --- a/config/keys.go +++ b/config/keys.go @@ -13,6 +13,7 @@ const ( ConfigContentKey = "config-file-content" ConfigContentTypeKey = "config-file-content-type" VersionKey = "version" + VersionJSONKey = "version-json" GenesisFileKey = "genesis-file" GenesisFileContentKey = "genesis-file-content" NetworkNameKey = "network-id" diff --git a/main/main.go b/main/main.go index 5651bf61940f..88de72dadbbe 100644 --- a/main/main.go +++ b/main/main.go @@ -4,6 +4,7 @@ package main import ( + "encoding/json" "errors" "fmt" "os" @@ -29,8 +30,24 @@ func main() { os.Exit(1) } + if v.GetBool(config.VersionJSONKey) && v.GetBool(config.VersionKey) { + fmt.Println("can't print both JSON and human readable versions") + os.Exit(1) + } + + if v.GetBool(config.VersionJSONKey) { + versions := version.GetVersions() + jsonBytes, err := json.MarshalIndent(versions, "", " ") + if err != nil { + fmt.Printf("couldn't marshal versions: %s\n", err) + os.Exit(1) + } + fmt.Println(string(jsonBytes)) + os.Exit(0) + } + if v.GetBool(config.VersionKey) { - fmt.Print(version.String) + fmt.Println(version.GetVersions().String()) os.Exit(0) } diff --git a/version/string.go b/version/string.go index 9abe555bcebb..80df9bea697a 100644 --- a/version/string.go +++ b/version/string.go @@ -9,32 +9,37 @@ import ( "strings" ) -var ( - // String is displayed when CLI arg --version is used - String string +// GitCommit is set in the build script at compile time +var GitCommit string - // GitCommit is set in the build script at compile time - GitCommit string -) +// Versions contains the versions relevant to a build of avalanchego. In +// addition to supporting construction of the string displayed by +// --version, it is used to produce the output of --version-json and can +// be used to unmarshal that output. +type Versions struct { + Application string `json:"application"` + Database string `json:"database"` + RPCChainVM uint64 `json:"rpcchainvm"` + // Commit may be empty if GitCommit was not set at compile time + Commit string `json:"commit"` + Go string `json:"go"` +} -func init() { - format := "%s [database=%s, rpcchainvm=%d" - args := []interface{}{ - CurrentApp, - CurrentDatabase, - RPCChainVMProtocol, - } - if GitCommit != "" { - format += ", commit=%s" - args = append(args, GitCommit) +func GetVersions() *Versions { + return &Versions{ + Application: CurrentApp.String(), + Database: CurrentDatabase.String(), + RPCChainVM: uint64(RPCChainVMProtocol), + Commit: GitCommit, + Go: strings.TrimPrefix(runtime.Version(), "go"), } +} - // add golang version - goVersion := runtime.Version() - goVersionNumber := strings.TrimPrefix(goVersion, "go") - format += ", go=%s" - args = append(args, goVersionNumber) - - format += "]\n" - String = fmt.Sprintf(format, args...) +func (v *Versions) String() string { + // This format maintains consistency with previous --version output + versionString := fmt.Sprintf("%s [database=%s, rpcchainvm=%d, ", v.Application, v.Database, v.RPCChainVM) + if len(v.Commit) > 0 { + versionString += fmt.Sprintf("commit=%s, ", v.Commit) + } + return versionString + fmt.Sprintf("go=%s]", v.Go) } diff --git a/version/string_test.go b/version/string_test.go new file mode 100644 index 000000000000..58f44668b3e0 --- /dev/null +++ b/version/string_test.go @@ -0,0 +1,23 @@ +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package version + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVersionsGetString(t *testing.T) { + versions := Versions{ + Application: "1", + Database: "2", + RPCChainVM: 3, + Commit: "4", + Go: "5", + } + require.Equal(t, "1 [database=2, rpcchainvm=3, commit=4, go=5]", versions.String()) + versions.Commit = "" + require.Equal(t, "1 [database=2, rpcchainvm=3, go=5]", versions.String()) +}