-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version prints out short info by default
Handle -o json, add --long flag to print full version info. Closes: #1894
- Loading branch information
Alessio Treglia
committed
Jan 29, 2019
1 parent
90797f5
commit 22e13ff
Showing
4 changed files
with
58 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/tendermint/tendermint/libs/cli" | ||
) | ||
|
||
const ( | ||
flagLong = "long" | ||
) | ||
|
||
var ( | ||
|
||
// VersionCmd prints out the current sdk version | ||
VersionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the app version", | ||
Run: printVersion, | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
verInfo := newVersionInfo() | ||
|
||
if !viper.GetBool(flagLong) { | ||
fmt.Println(verInfo.CosmosSDK) | ||
return nil | ||
} | ||
|
||
if viper.GetString(cli.OutputFlag) != "json" { | ||
fmt.Print(verInfo) | ||
return nil | ||
} | ||
|
||
bz, err := json.Marshal(verInfo) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(bz)) | ||
return nil | ||
}, | ||
} | ||
) | ||
|
||
// return version of CLI/node and commit hash | ||
func GetVersion() string { | ||
return Version | ||
} | ||
|
||
// CMD | ||
func printVersion(cmd *cobra.Command, args []string) { | ||
fmt.Println("cosmos-sdk:", GetVersion()) | ||
fmt.Println("git commit:", Commit) | ||
fmt.Println("vendor hash:", VendorDirHash) | ||
fmt.Printf("go version %s %s/%s\n", | ||
runtime.Version(), runtime.GOOS, runtime.GOARCH) | ||
func init() { | ||
VersionCmd.Flags().Bool(flagLong, false, "Print long version information") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
//nolint | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
// Variables set by build flags | ||
var ( | ||
Commit = "" | ||
Version = "" | ||
VendorDirHash = "" | ||
) | ||
|
||
type versionInfo struct { | ||
CosmosSDK string `json:"cosmos_sdk"` | ||
GitCommit string `json:"commit"` | ||
VendorDirHash string `json:"vendor_hash"` | ||
GoVersion string `json:"go"` | ||
} | ||
|
||
func (v versionInfo) String() string { | ||
return fmt.Sprintf(`cosmos-sdk: %s | ||
git commit: %s | ||
vendor hash: %s | ||
%s`, v.CosmosSDK, v.GitCommit, v.VendorDirHash, v.GoVersion) | ||
} | ||
|
||
func newVersionInfo() versionInfo { | ||
return versionInfo{ | ||
Version, Commit, VendorDirHash, fmt.Sprintf("go version %s %s/%s\n", | ||
runtime.Version(), runtime.GOOS, runtime.GOARCH)} | ||
} |