Skip to content

Commit

Permalink
Emit version in JSON format for --json-version (#3129)
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Jun 21, 2024
1 parent d0c2094 commit cafd71c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
1 change: 1 addition & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 18 additions & 1 deletion main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -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)
}

Expand Down
53 changes: 29 additions & 24 deletions version/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
23 changes: 23 additions & 0 deletions version/string_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit cafd71c

Please sign in to comment.