Skip to content

Commit

Permalink
feat(ioctl): bc version (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun authored and dustinxie committed Jan 29, 2024
1 parent b583c1b commit c85ee31
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ioctl/cmd/bc/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions ioctl/cmd/bc/bcversion.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c85ee31

Please sign in to comment.