From 322388f1be67498199df300a456f05aba8187563 Mon Sep 17 00:00:00 2001 From: saito Date: Tue, 9 Jan 2024 09:17:23 +0800 Subject: [PATCH] feat(ioctl): bc version (#4005) --- ioctl/cmd/bc/bc.go | 6 ++-- ioctl/cmd/bc/bcversion.go | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 ioctl/cmd/bc/bcversion.go diff --git a/ioctl/cmd/bc/bc.go b/ioctl/cmd/bc/bc.go index c5d9462a50..dd47c54095 100644 --- a/ioctl/cmd/bc/bc.go +++ b/ioctl/cmd/bc/bc.go @@ -10,14 +10,13 @@ import ( "strconv" "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" + "github.com/iotexproject/iotex-proto/golang/iotexapi" + "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/spf13/cobra" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-proto/golang/iotexapi" - "github.com/iotexproject/iotex-proto/golang/iotextypes" - "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" @@ -51,6 +50,7 @@ func init() { BCCmd.AddCommand(_bcBucketListCmd) BCCmd.AddCommand(_bcBucketCmd) BCCmd.AddCommand(_bcDelegateCmd) + BCCmd.AddCommand(_bcVersionCmd) BCCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint", config.ReadConfig.Endpoint, config.TranslateInLang(_flagEndpointUsages, config.UILanguage)) BCCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure, diff --git a/ioctl/cmd/bc/bcversion.go b/ioctl/cmd/bc/bcversion.go new file mode 100644 index 0000000000..1ca38aeb95 --- /dev/null +++ b/ioctl/cmd/bc/bcversion.go @@ -0,0 +1,65 @@ +package bc + +import ( + "context" + "fmt" + + "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" + "github.com/iotexproject/iotex-proto/golang/iotexapi" + "github.com/spf13/cobra" + "google.golang.org/grpc/status" + + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" + "github.com/iotexproject/iotex-core/ioctl/util" +) + +var versionCmdShorts = map[config.Language]string{ + config.English: "get blockchain version", + config.Chinese: "获取API节点版本信息", +} + +var _bcVersionCmd = &cobra.Command{ + Use: "version", + Short: config.TranslateInLang(versionCmdShorts, config.UILanguage), + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + out, err := bcVersion() + if err != nil { + return output.PrintError(err) + } + output.PrintResult(out) + return nil + }, +} + +func bcVersion() (string, error) { + conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure) + if err != nil { + return "", output.NewError(output.NetworkError, "failed to connect to endpoint", err) + } + defer conn.Close() + + cli := iotexapi.NewAPIServiceClient(conn) + req := &iotexapi.GetServerMetaRequest{} + ctx := context.Background() + + jwtMD, err := util.JwtAuth() + if err == nil { + ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx) + } + + rsp, err := cli.GetServerMeta(ctx, req) + if err != nil { + sta, ok := status.FromError(err) + if ok { + return "", output.NewError(output.APIError, sta.Message(), nil) + } + return "", output.NewError(output.NetworkError, "failed to get version from server", err) + } + + title := "API endpoint version" + detail := output.JSONString(rsp.ServerMeta) + + return fmt.Sprintf("%s:\n%s\n", title, detail), nil +}