code.vegaprotocol.io/vega@v0.79.0/cmd/visor/version.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  
    21  	vgjson "code.vegaprotocol.io/vega/libs/json"
    22  	"code.vegaprotocol.io/vega/version"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  const (
    28  	outputFlagName     = "output"
    29  	outputFlagValJSON  = "json"
    30  	outputFlagValHuman = "human"
    31  )
    32  
    33  func init() {
    34  	rootCmd.AddCommand(versionCmd)
    35  	versionCmd.Flags().String(outputFlagName, outputFlagValHuman, "Specify the output format: json,human")
    36  }
    37  
    38  var versionCmd = &cobra.Command{
    39  	Use:          "version",
    40  	Short:        "Returns a Vega Visor version",
    41  	SilenceUsage: false,
    42  	RunE: func(cmd *cobra.Command, args []string) error {
    43  		output, err := cmd.Flags().GetString(outputFlagName)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		switch output {
    49  		case outputFlagValHuman:
    50  			fmt.Printf("Vega Visor CLI %s (%s)\n", version.Get(), version.GetCommitHash())
    51  			return nil
    52  		case outputFlagValJSON:
    53  			return vgjson.Print(struct {
    54  				Version string `json:"version"`
    55  				Hash    string `json:"hash"`
    56  			}{
    57  				Version: version.Get(),
    58  				Hash:    version.GetCommitHash(),
    59  			})
    60  		default:
    61  			return fmt.Errorf("%s flag must be either %q or %q", outputFlagName, outputFlagValHuman, outputFlagValJSON)
    62  		}
    63  	},
    64  }