-
Notifications
You must be signed in to change notification settings - Fork 2
/
version.go
79 lines (63 loc) · 1.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"runtime"
"github.com/sirkon/goproxy/gomod"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
const (
flagFormat = "format"
)
// Version defines the application version (defined at compile time)
// GitCommit defines the application commit hash (defined at compile time)
var (
Version = ""
Commit = ""
versionFormat string
)
type versionInfo struct {
Version string `json:"version" yaml:"version"`
Commit string `json:"commit" yaml:"commit"`
SDK string `json:"sdk" yaml:"sdk"`
Go string `json:"go" yaml:"go"`
}
func getVersionCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "Print the version of Dessert",
RunE: func(cmd *cobra.Command, args []string) error {
modBz, err := ioutil.ReadFile("go.mod")
if err != nil {
return err
}
mod, err := gomod.Parse("go.mod", modBz)
if err != nil {
return err
}
verInfo := versionInfo{
Version: Version,
Commit: Commit,
SDK: mod.Require["github.com/cosmos/cosmos-sdk"],
Go: fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH),
}
var bz []byte
switch versionFormat {
case "json":
bz, err = json.Marshal(verInfo)
default:
bz, err = yaml.Marshal(&verInfo)
}
if err != nil {
return err
}
_, err = fmt.Println(string(bz))
return err
},
}
versionCmd.Flags().StringVar(&versionFormat, flagFormat, "text", "Print the version in the given format (text | json)")
return versionCmd
}