forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
85 lines (65 loc) · 1.76 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
81
82
83
84
85
package cmd
import (
"fmt"
"github.com/Azure/acs-engine/pkg/helpers"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
// BuildTag holds the `git tag` if this is a tagged build/release
BuildTag = "canary"
// BuildSHA holds the git commit SHA at `make build` time.
BuildSHA = "unset"
// GitTreeState is the state of the git tree, either clean or dirty
GitTreeState = "unset"
outputFormatOptions = []string{"human", "json"}
outputFormat string
version versionInfo
)
type versionInfo struct {
GitTag string
GitCommit string
GitTreeState string
}
func init() {
version = versionInfo{
GitTag: BuildTag,
GitCommit: BuildSHA,
GitTreeState: GitTreeState,
}
}
func getHumanVersion() string {
r := fmt.Sprintf("Version: %s\nGitCommit: %s\nGitTreeState: %s",
version.GitTag,
version.GitCommit,
version.GitTreeState)
return r
}
func getJSONVersion() string {
jsonVersion, _ := helpers.JSONMarshalIndent(version, "", " ", false)
return string(jsonVersion)
}
func getVersion(outputType string) string {
var output string
if outputType == "human" {
output = getHumanVersion()
} else if outputType == "json" {
output = getJSONVersion()
} else {
log.Fatalf("unsupported output format: %s\n", outputFormat)
}
return output
}
func newVersionCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version of ACS-Engine",
Long: "Print the version of ACS-Engine",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getVersion(outputFormat))
},
}
versionCmdDescription := fmt.Sprintf("Output format to use: %s", outputFormatOptions)
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "human", versionCmdDescription)
return versionCmd
}