-
Notifications
You must be signed in to change notification settings - Fork 23
/
version.go
59 lines (48 loc) · 1.17 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
package cmd
import (
"fmt"
"runtime/debug"
"github.com/0chain/zboxcli/util"
"github.com/icza/bitio"
"github.com/spf13/cobra"
)
var VersionStr string
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints version information",
Long: `Prints version information`,
Run: func(cmd *cobra.Command, args []string) {
doJSON, _ := cmd.Flags().GetBool("json")
if doJSON {
j := make(map[string]string)
j["zbox"] = VersionStr
j["gosdk"] = getVersion("github.com/0chain/gosdk")
util.PrintJSON(j)
return
}
fmt.Println("Version info:")
fmt.Println("\tzbox....: ", VersionStr)
fmt.Println("\tgosdk...: ", getVersion("github.com/0chain/gosdk"))
},
}
func getVersion(path string) string {
_ = bitio.NewReader
bi, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("Failed to read build info")
return ""
}
for _, dep := range bi.Deps {
if dep.Path == path {
if dep.Replace != nil && dep.Replace.Version != "" {
return dep.Replace.Version
}
return dep.Version
}
}
return ""
}
func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().Bool("json", false, "(default false) pass this option to print response as json data")
}