/
version.go
72 lines (58 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
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
goVersion "go.hein.dev/go-version"
)
// getVersion return the sensor's VersionInfo details.
func getVersion() *Info {
versionOutput := goVersion.New(version, commit, date)
output := &Info{
Version: versionOutput.Version,
Commit: versionOutput.Commit,
Date: versionOutput.Date,
}
return output
}
// toJSON converts the Info into a JSON String.
func (v *Info) toJSON() string {
bytes, _ := json.Marshal(v)
return string(bytes)
}
// toShortened converts the Version into a String.
func (v *Info) toShortened() string {
return fmt.Sprintf("Version: %s\n", v.Version)
}
// versionCmd represents the version command.
var (
shortened = false
version = "dev"
commit = "none"
date = "unknown"
output = "json"
versionCmd = &cobra.Command{
Use: "version",
Short: "Display the version of tool",
Run: func(cmd *cobra.Command, args []string) {
var response string
var resourceFilesInfo []*ResourceFilesInfo
versionInfo := getVersion()
// ignoring error here for now
resourceFilesInfo, _ = getAllEmbeddedResourceFiles()
versionInfo.ResourceFiles = resourceFilesInfo
if shortened {
response = versionInfo.toShortened()
} else {
response = versionInfo.toJSON()
}
fmt.Printf("%s\n", response)
},
}
)
func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
versionCmd.Flags().
StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
}