github.com/MetalBlockchain/metalgo@v1.11.9/version/string.go (about)

     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  	"fmt"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // GitCommit is set in the build script at compile time
    13  var GitCommit string
    14  
    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  }
    27  
    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"),
    35  	}
    36  }
    37  
    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)
    45  }