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