44package cmd
55
66import (
7+ "encoding/json"
78 "fmt"
89
910 "github.com/spf13/cobra"
@@ -14,36 +15,59 @@ import (
1415)
1516
1617var versionVerbose bool
18+ var versionJSON bool
1719
1820// versionCmd represents the version command
1921var versionCmd = & cobra.Command {
20- Use : "version [-v]" ,
22+ Use : "version [-v] [--json] " ,
2123 Short : "Print the version number of wsh" ,
2224 RunE : runVersionCmd ,
2325}
2426
2527func init () {
2628 versionCmd .Flags ().BoolVarP (& versionVerbose , "verbose" , "v" , false , "Display full version information" )
29+ versionCmd .Flags ().BoolVar (& versionJSON , "json" , false , "Output version information in JSON format" )
2730 rootCmd .AddCommand (versionCmd )
2831}
2932
3033func runVersionCmd (cmd * cobra.Command , args []string ) error {
31- if ! versionVerbose {
34+ if ! versionVerbose && ! versionJSON {
3235 WriteStdout ("wsh v%s\n " , wavebase .WaveVersion )
3336 return nil
3437 }
38+
3539 err := preRunSetupRpcClient (cmd , args )
3640 if err != nil {
3741 return err
3842 }
43+
3944 resp , err := wshclient .WaveInfoCommand (RpcClient , & wshrpc.RpcOpts {Timeout : 2000 })
4045 if err != nil {
4146 return err
4247 }
48+
4349 updateChannel , err := wshclient .GetUpdateChannelCommand (RpcClient , & wshrpc.RpcOpts {Timeout : 2000 , Route : wshutil .ElectronRoute })
4450 if err != nil {
4551 return err
4652 }
53+
54+ if versionJSON {
55+ info := map [string ]interface {}{
56+ "version" : resp .Version ,
57+ "buildtime" : resp .BuildTime ,
58+ "configdir" : resp .ConfigDir ,
59+ "datadir" : resp .DataDir ,
60+ "updatechannel" : updateChannel ,
61+ }
62+ outBArr , err := json .MarshalIndent (info , "" , " " )
63+ if err != nil {
64+ return fmt .Errorf ("formatting version info: %v" , err )
65+ }
66+ WriteStdout ("%s\n " , string (outBArr ))
67+ return nil
68+ }
69+
70+ // Default verbose text output
4771 fmt .Printf ("v%s (%s)\n " , resp .Version , resp .BuildTime )
4872 fmt .Printf ("configdir: %s\n " , resp .ConfigDir )
4973 fmt .Printf ("datadir: %s\n " , resp .DataDir )
0 commit comments