github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/bc/bcinfo.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 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 "fmt" 10 11 "github.com/spf13/cobra" 12 13 "github.com/iotexproject/iotex-proto/golang/iotextypes" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 ) 18 19 // Multi-language support 20 var ( 21 _bcInfoCmdShorts = map[config.Language]string{ 22 config.English: "Get current blockchain information", 23 config.Chinese: "获取当前区块链信息", 24 } 25 ) 26 27 // _bcInfoCmd represents the bc info command 28 var _bcInfoCmd = &cobra.Command{ 29 Use: "info", 30 Short: config.TranslateInLang(_bcInfoCmdShorts, config.UILanguage), 31 Args: cobra.ExactArgs(0), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 cmd.SilenceUsage = true 34 err := bcInfo() 35 return output.PrintError(err) 36 }, 37 } 38 39 type infoMessage struct { 40 Node string `json:"node"` 41 Info *iotextypes.ChainMeta `json:"info"` 42 } 43 44 func (m *infoMessage) String() string { 45 if output.Format == "" { 46 message := fmt.Sprintf("Blockchain Node: %s\n%s", m.Node, output.JSONString(m.Info)) 47 return message 48 } 49 return output.FormatString(output.Result, m) 50 } 51 52 // bcInfo get current information of blockchain from server 53 func bcInfo() error { 54 chainMeta, err := GetChainMeta() 55 if err != nil { 56 return output.NewError(0, "failed to get chain meta", err) 57 } 58 message := infoMessage{Node: config.ReadConfig.Endpoint, Info: chainMeta} 59 fmt.Println(message.String()) 60 return nil 61 }