github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/version/version.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 version 7 8 import ( 9 _ "embed" // embed ioctl standalone version file 10 "fmt" 11 12 "github.com/iotexproject/iotex-proto/golang/iotextypes" 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 ver "github.com/iotexproject/iotex-core/pkg/version" 18 ) 19 20 // Multi-language support 21 var ( 22 _versionCmdShorts = map[config.Language]string{ 23 config.English: "Print the version of ioctl and node", 24 config.Chinese: "打印ioctl和节点的版本", 25 } 26 _flagEndpointUsage = map[config.Language]string{ 27 config.English: "set endpoint for once", 28 config.Chinese: "一次设置端点", 29 } 30 _flagInsecureUsage = map[config.Language]string{ 31 config.English: "insecure connection for once", 32 config.Chinese: "一次不安全的连接", 33 } 34 //go:embed version 35 _ioctlStandaloneVersion string 36 ) 37 38 // VersionCmd represents the version command 39 var VersionCmd = &cobra.Command{ 40 Use: "version", 41 Short: config.TranslateInLang(_versionCmdShorts, config.UILanguage), 42 Args: cobra.ExactArgs(0), 43 RunE: func(cmd *cobra.Command, args []string) error { 44 cmd.SilenceUsage = true 45 err := version() 46 return err 47 }, 48 } 49 50 type versionMessage struct { 51 Object string `json:"object"` 52 VersionInfo *iotextypes.ServerMeta `json:"versionInfo"` 53 } 54 55 func init() { 56 VersionCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint", 57 config.ReadConfig.Endpoint, config.TranslateInLang(_flagEndpointUsage, config.UILanguage)) 58 VersionCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure, 59 config.TranslateInLang(_flagInsecureUsage, config.UILanguage)) 60 } 61 62 func version() error { 63 fmt.Printf("Version: %s\n\n", _ioctlStandaloneVersion) 64 65 message := versionMessage{} 66 67 message.Object = "Build Info" 68 message.VersionInfo = &iotextypes.ServerMeta{ 69 PackageVersion: ver.PackageVersion, 70 PackageCommitID: ver.PackageCommitID, 71 GitStatus: ver.GitStatus, 72 GoVersion: ver.GoVersion, 73 BuildTime: ver.BuildTime, 74 } 75 fmt.Println(message.String()) 76 return nil 77 } 78 79 func (m *versionMessage) String() string { 80 if output.Format == "" { 81 return fmt.Sprintf("%s:\n%+v\n", m.Object, m.VersionInfo) 82 } 83 return output.FormatString(output.Result, m) 84 }