-
Notifications
You must be signed in to change notification settings - Fork 3
/
version.go
53 lines (45 loc) · 966 Bytes
/
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
package cmd
import (
"fmt"
"runtime/debug"
"github.com/spf13/cobra"
)
func getVersion() string {
if version != "unknown" {
return version
}
if bi, ok := debug.ReadBuildInfo(); ok {
var vcs struct {
sys string
rev string
}
for _, setting := range bi.Settings {
switch setting.Key {
case "vcs":
vcs.sys = setting.Value
case "vcs.revision":
vcs.rev = fmt.Sprintf("%.7s", setting.Value)
case "vcs.modified":
if setting.Value == "true" {
vcs.rev = fmt.Sprintf("%s-dirty", vcs.rev)
}
}
}
if vcs.sys != "" {
return fmt.Sprintf("%s vcs: %s:%s\n", bi.Main.Version, vcs.sys, vcs.rev)
}
return fmt.Sprintf(bi.Main.Version)
}
return "unknown"
}
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show cape version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getVersion())
},
}
func init() {
rootCmd.AddCommand(versionCmd)
}