Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit f7a8edd

Browse files
sgoingsjackfrancis
authored andcommitted
version output improvements (#1325)
- output types supported: human, json
1 parent 58090fa commit f7a8edd

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

cmd/version.go

+61-3
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,85 @@
11
package cmd
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
47
log "github.com/sirupsen/logrus"
8+
59
"github.com/spf13/cobra"
610
)
711

812
var (
13+
// BuildTag holds the `git tag` if this is a tagged build/release
14+
BuildTag = "canary"
15+
916
// BuildSHA holds the git commit SHA at `make build` time.
1017
BuildSHA = "unset"
1118

12-
// BuildTag holds the `git tag` if this is a tagged build/release
13-
BuildTag = "unset"
19+
// GitTreeState is the state of the git tree, either clean or dirty
20+
GitTreeState = "unset"
21+
22+
outputFormatOptions = []string{"human", "json"}
23+
outputFormat string
24+
version versionInfo
1425
)
1526

27+
type versionInfo struct {
28+
GitTag string
29+
GitCommit string
30+
GitTreeState string
31+
}
32+
33+
func init() {
34+
version = versionInfo{
35+
GitTag: BuildTag,
36+
GitCommit: BuildSHA,
37+
GitTreeState: GitTreeState,
38+
}
39+
}
40+
41+
func getHumanVersion() string {
42+
r := fmt.Sprintf("Version: %s\nGitCommit: %s\nGitTreeState: %s",
43+
version.GitTag,
44+
version.GitCommit,
45+
version.GitTreeState)
46+
47+
return r
48+
}
49+
50+
func getJSONVersion() string {
51+
jsonVersion, _ := json.MarshalIndent(version, "", " ")
52+
return string(jsonVersion)
53+
}
54+
55+
func getVersion(outputType string) string {
56+
var output string
57+
58+
if outputType == "human" {
59+
output = getHumanVersion()
60+
} else if outputType == "json" {
61+
output = getJSONVersion()
62+
} else {
63+
log.Fatalf("unsupported output format: %s\n", outputFormat)
64+
}
65+
66+
return output
67+
}
68+
1669
func newVersionCmd() *cobra.Command {
1770
versionCmd := &cobra.Command{
1871
Use: "version",
1972
Short: "Print the version of ACS-Engine",
2073
Long: "Print the version of ACS-Engine",
2174

2275
Run: func(cmd *cobra.Command, args []string) {
23-
log.Infof("ACS-Engine Version: %s (%s)", BuildTag, BuildSHA)
76+
fmt.Println(getVersion(outputFormat))
2477
},
2578
}
79+
80+
versionCmdDescription := fmt.Sprintf("Output format to use: %s", outputFormatOptions)
81+
82+
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "human", versionCmdDescription)
83+
2684
return versionCmd
2785
}

cmd/version_test.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
. "github.com/onsi/ginkgo"
78
. "github.com/onsi/gomega"
8-
logtest "github.com/sirupsen/logrus/hooks/test"
99
)
1010

1111
var _ = Describe("the version command", func() {
12-
It("should print the version of ACS-Engine", func() {
13-
command := newVersionCmd()
14-
hook := logtest.NewGlobal()
15-
command.Run(command, nil)
16-
Expect(hook.LastEntry().Message).To(Equal(fmt.Sprintf("ACS-Engine Version: %s (%s)", BuildTag, BuildSHA)))
12+
It("should print a humanized version of ACS-Engine", func() {
13+
output := getVersion("human")
14+
15+
expectedOutput := fmt.Sprintf("Version: %s\nGitCommit: %s\nGitTreeState: %s",
16+
BuildTag,
17+
BuildSHA,
18+
GitTreeState)
19+
20+
Expect(output).Should(Equal(expectedOutput))
21+
})
22+
23+
It("should print a json version of ACS-Engine", func() {
24+
output := getVersion("json")
25+
26+
expectedOutput, _ := json.MarshalIndent(version, "", " ")
27+
28+
Expect(output).Should(Equal(string(expectedOutput)))
1729
})
1830
})

versioning.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ GIT_SHA = $(shell git rev-parse --short HEAD)
33
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null || echo "canary")
44
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
55

6-
LDFLAGS += -X github.com/Azure/acs-engine/cmd.BuildSHA=${GIT_SHA}-${GIT_DIRTY}
6+
LDFLAGS += -X github.com/Azure/acs-engine/cmd.BuildSHA=${GIT_SHA}
7+
LDFLAGS += -X github.com/Azure/acs-engine/cmd.GitTreeState=${GIT_DIRTY}
78
DOCKER_VERSION ?= git-${GIT_SHA}
89

910
ifneq ($(GIT_TAG),)

0 commit comments

Comments
 (0)