-
Notifications
You must be signed in to change notification settings - Fork 791
/
version.go
80 lines (63 loc) · 1.55 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
80
package cmd
import (
"encoding/json"
"fmt"
"os"
"runtime"
"github.com/sirkon/goproxy/gomod"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
const (
flagFormat = "format"
pathCosmosSDK = "github.com/cosmos/cosmos-sdk"
)
var (
// Version defines the application version (defined at compile time)
Version = ""
// Commit defines the application commit hash (defined at compile time)
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",
Short: "Print binary version information",
RunE: func(cmd *cobra.Command, args []string) error {
modBz, err := os.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[pathCosmosSDK],
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
}