/
bcinfo.go
61 lines (51 loc) · 1.72 KB
/
bcinfo.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
// Copyright (c) 2022 IoTeX Foundation
// 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 bc
import (
"fmt"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
)
// Multi-language support
var (
_bcInfoCmdShorts = map[config.Language]string{
config.English: "Get current blockchain information",
config.Chinese: "获取当前区块链信息",
}
)
// _bcInfoCmd represents the bc info command
var _bcInfoCmd = &cobra.Command{
Use: "info",
Short: config.TranslateInLang(_bcInfoCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := bcInfo()
return output.PrintError(err)
},
}
type infoMessage struct {
Node string `json:"node"`
Info *iotextypes.ChainMeta `json:"info"`
}
func (m *infoMessage) String() string {
if output.Format == "" {
message := fmt.Sprintf("Blockchain Node: %s\n%s", m.Node, output.JSONString(m.Info))
return message
}
return output.FormatString(output.Result, m)
}
// bcInfo get current information of blockchain from server
func bcInfo() error {
chainMeta, err := GetChainMeta()
if err != nil {
return output.NewError(0, "failed to get chain meta", err)
}
message := infoMessage{Node: config.ReadConfig.Endpoint, Info: chainMeta}
fmt.Println(message.String())
return nil
}