github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bcinfo.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 bc 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "log" 12 13 "github.com/iotexproject/iotex-proto/golang/iotextypes" 14 "github.com/spf13/cobra" 15 16 "github.com/iotexproject/iotex-core/ioctl" 17 "github.com/iotexproject/iotex-core/ioctl/config" 18 ) 19 20 // Multi-language support 21 var ( 22 _bcInfoCmdShorts = map[config.Language]string{ 23 config.English: "Get current block chain information", 24 config.Chinese: "获取当前区块链信息", 25 } 26 ) 27 28 type infoMessage struct { 29 Node string `json:"node"` 30 Info *iotextypes.ChainMeta `json:"info"` 31 } 32 33 // NewBCInfoCmd represents the bc info command 34 func NewBCInfoCmd(client ioctl.Client) *cobra.Command { 35 bcInfoCmdShort, _ := client.SelectTranslation(_bcInfoCmdShorts) 36 return &cobra.Command{ 37 Use: "info", 38 Short: bcInfoCmdShort, 39 Args: cobra.ExactArgs(0), 40 RunE: func(cmd *cobra.Command, args []string) error { 41 cmd.SilenceUsage = true 42 chainMeta, err := GetChainMeta(client) 43 if err != nil { 44 return err 45 } 46 47 message := infoMessage{Node: client.Config().Endpoint, Info: chainMeta} 48 cmd.Println(fmt.Sprintf("Blockchain Node: %s\n%s", message.Node, JSONString(message.Info))) 49 return nil 50 }, 51 } 52 } 53 54 // JSONString returns json string for message 55 func JSONString(out interface{}) string { 56 byteAsJSON, err := json.MarshalIndent(out, "", " ") 57 if err != nil { 58 log.Panic(err) 59 } 60 return fmt.Sprint(string(byteAsJSON)) 61 }