github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/version/version.go (about)

     1  // Copyright (c) 2022 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package version
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
    12  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
    13  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    14  	"github.com/pkg/errors"
    15  	"github.com/spf13/cobra"
    16  	"google.golang.org/grpc/codes"
    17  	"google.golang.org/grpc/status"
    18  
    19  	"github.com/iotexproject/iotex-core/ioctl"
    20  	"github.com/iotexproject/iotex-core/ioctl/config"
    21  	"github.com/iotexproject/iotex-core/ioctl/util"
    22  	ver "github.com/iotexproject/iotex-core/pkg/version"
    23  )
    24  
    25  // Multi-language support
    26  var (
    27  	_shorts = map[config.Language]string{
    28  		config.English: "Print the version of ioctl and node",
    29  		config.Chinese: "打印ioctl和节点的版本号",
    30  	}
    31  )
    32  
    33  // NewVersionCmd represents the version command
    34  func NewVersionCmd(cli ioctl.Client) *cobra.Command {
    35  	short, _ := cli.SelectTranslation(_shorts)
    36  	vc := &cobra.Command{
    37  		Use:   "version",
    38  		Short: short,
    39  		Args:  cobra.ExactArgs(0),
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			cmd.SilenceUsage = true
    42  			cmd.Printf("Client:\n%+v\n", &iotextypes.ServerMeta{
    43  				PackageVersion:  ver.PackageVersion,
    44  				PackageCommitID: ver.PackageCommitID,
    45  				GitStatus:       ver.GitStatus,
    46  				GoVersion:       ver.GoVersion,
    47  				BuildTime:       ver.BuildTime,
    48  			})
    49  			apiClient, err := cli.APIServiceClient()
    50  			if err != nil {
    51  				return err
    52  			}
    53  
    54  			jwtMD, err := util.JwtAuth()
    55  			var ctx context.Context
    56  			if err == nil {
    57  				ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
    58  			}
    59  			response, err := apiClient.GetServerMeta(
    60  				ctx,
    61  				&iotexapi.GetServerMetaRequest{},
    62  			)
    63  			if err != nil {
    64  				if sta, ok := status.FromError(err); ok {
    65  					if sta.Code() == codes.Unavailable {
    66  						return ioctl.ErrInvalidEndpointOrInsecure
    67  					}
    68  					return errors.New(sta.Message())
    69  				}
    70  				return errors.Wrap(err, "failed to get version from server")
    71  			}
    72  			cmd.Printf("%s:\n%+v\n", cli.Config().Endpoint, response.ServerMeta)
    73  			return nil
    74  		},
    75  	}
    76  
    77  	cli.SetEndpointWithFlag(vc.PersistentFlags().StringVar)
    78  	cli.SetInsecureWithFlag(vc.PersistentFlags().BoolVar)
    79  	return vc
    80  }