Skip to content

Commit 9a24c27

Browse files
Merge pull request #14 from christopherhein/bug/fixed-func
Fixed func since it's not eval'ed each time
2 parents 142395b + a57e043 commit 9a24c27

File tree

7 files changed

+61
-44
lines changed

7 files changed

+61
-44
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.0.2
1+
v0.0.3

example/version.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616
package main
1717

1818
import (
19-
"os"
19+
"fmt"
2020

2121
"github.com/spf13/cobra"
2222
goVersion "go.hein.dev/go-version"
@@ -27,15 +27,21 @@ var (
2727
version = "dev"
2828
commit = "none"
2929
date = "unknown"
30+
output = "json"
3031
versionCmd = &cobra.Command{
3132
Use: "version",
3233
Short: "Version will output the current build information",
3334
Long: ``,
34-
Run: goVersion.Func(os.Stdout, shortened, version, commit, date),
35+
Run: func(_ *cobra.Command, _ []string) {
36+
resp := goVersion.FuncWithOutput(shortened, version, commit, date, output)
37+
fmt.Print(resp)
38+
return
39+
},
3540
}
3641
)
3742

3843
func init() {
39-
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
44+
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
45+
versionCmd.Flags().StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
4046
rootCmd.AddCommand(versionCmd)
4147
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ require (
1212
golang.org/x/sys v0.0.0-20190812172437-4e8604ab3aff // indirect
1313
golang.org/x/text v0.3.2 // indirect
1414
golang.org/x/tools v0.0.0-20190812233024-afc3694995b6 // indirect
15+
sigs.k8s.io/yaml v1.1.0
1516
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
160160
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
161161
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
162162
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
163+
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
164+
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=

readme.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ var (
2626
version = "dev"
2727
commit = "none"
2828
date = "unknown"
29+
output = "json"
2930
versionCmd = &cobra.Command{
3031
Use: "version",
3132
Short: "Version will output the current build information",
3233
Long: ``,
33-
Run: goVersion.Func(shortened, version, commit, date),
34+
Run: func(_ *cobra.Command, _ []string) {
35+
resp := goVersion.FuncWithOutput(shortened, version, commit, date, output)
36+
fmt.Print(resp)
37+
return
38+
},
3439
}
3540
)
3641
3742
func init() {
38-
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
43+
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
44+
versionCmd.Flags().StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
3945
rootCmd.AddCommand(versionCmd)
4046
}
4147
----

version.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package version // import "go.hein.dev/go-version"
33
import (
44
"encoding/json"
55
"fmt"
6-
"io"
7-
"strings"
86

9-
"github.com/spf13/cobra"
7+
"sigs.k8s.io/yaml"
8+
)
9+
10+
var (
11+
// JSON returns json so that we can change the output
12+
JSON = "json"
13+
// YAML returns yaml so that we can change the output
14+
YAML = "yaml"
1015
)
1116

1217
// Info creates a formattable struct for output
@@ -25,20 +30,29 @@ func New(version string, commit string, date string) *Info {
2530
}
2631
}
2732

28-
// Func will add the versioning code
29-
func Func(out io.Writer, shortened bool, version, commit, date string) func(*cobra.Command, []string) {
30-
return func(_ *cobra.Command, _ []string) {
31-
var response string
32-
versionOutput := New(version, commit, date)
33+
// Func will return the versioning code with only JSON and raw text support
34+
func Func(shortened bool, version, commit, date string) string {
35+
return FuncWithOutput(shortened, version, commit, date, JSON)
36+
}
3337

34-
if shortened {
35-
response = versionOutput.ToShortened()
36-
} else {
38+
// FuncWithOutput will add the versioning code
39+
func FuncWithOutput(shortened bool, version, commit, date, output string) string {
40+
var response string
41+
versionOutput := New(version, commit, date)
42+
43+
if shortened {
44+
response = versionOutput.ToShortened()
45+
} else {
46+
switch output {
47+
case YAML:
48+
response = versionOutput.ToYAML()
49+
case JSON:
50+
response = versionOutput.ToJSON()
51+
default: // JSON as the default
3752
response = versionOutput.ToJSON()
3853
}
39-
fmt.Fprintf(out, "%+v", response)
40-
return
4154
}
55+
return fmt.Sprintf("%s", response)
4256
}
4357

4458
// ToJSON converts the Info into a JSON String
@@ -47,22 +61,15 @@ func (v *Info) ToJSON() string {
4761
return string(bytes) + "\n"
4862
}
4963

64+
// ToYAML converts the Info into a JSON String
65+
func (v *Info) ToYAML() string {
66+
bytes, _ := yaml.Marshal(v)
67+
return string(bytes)
68+
}
69+
5070
// ToShortened converts the Info into a JSON String
51-
func (v *Info) ToShortened() (str string) {
52-
var version, commit, date string
53-
if v.Version != "" {
54-
version = "Version: " + v.Version
55-
}
56-
if v.Commit != "" {
57-
commit = "Commit: " + v.Commit
58-
}
59-
if v.Date != "" {
60-
date = "Date: " + v.Date
61-
}
62-
values := []string{version, commit, date}
63-
values = deleteEmpty(values)
64-
str = strings.Join(values, "\n")
65-
return str + "\n"
71+
func (v *Info) ToShortened() string {
72+
return v.ToYAML()
6673
}
6774

6875
func deleteEmpty(s []string) []string {

version_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package version
22

33
import (
4-
"bytes"
54
"testing"
6-
7-
"github.com/spf13/cobra"
85
)
96

107
func TestToJSON(t *testing.T) {
@@ -21,9 +18,9 @@ func TestToShortened(t *testing.T) {
2118
v := New("dev", "fee8dd1f7c24da23508e26347694be0acce5631b", "Fri Jun 21 10:50:15 PDT 2019")
2219
short := v.ToShortened()
2320

24-
expected := `Version: dev
25-
Commit: fee8dd1f7c24da23508e26347694be0acce5631b
21+
expected := `Commit: fee8dd1f7c24da23508e26347694be0acce5631b
2622
Date: Fri Jun 21 10:50:15 PDT 2019
23+
Version: dev
2724
`
2825

2926
if short != expected {
@@ -43,18 +40,16 @@ func TestFunc(t *testing.T) {
4340
shortened: false,
4441
},
4542
testcase{
46-
expected: `Version: dev
47-
Commit: fee8dd1f7c24da23508e26347694be0acce5631b
43+
expected: `Commit: fee8dd1f7c24da23508e26347694be0acce5631b
4844
Date: Fri Jun 21 10:50:15 PDT 2019
45+
Version: dev
4946
`,
5047
shortened: true,
5148
},
5249
}
5350

5451
for _, c := range cases {
55-
buffer := &bytes.Buffer{}
56-
Func(buffer, c.shortened, "dev", "fee8dd1f7c24da23508e26347694be0acce5631b", "Fri Jun 21 10:50:15 PDT 2019")(&cobra.Command{}, []string{})
57-
resp := buffer.String()
52+
resp := FuncWithOutput(c.shortened, "dev", "fee8dd1f7c24da23508e26347694be0acce5631b", "Fri Jun 21 10:50:15 PDT 2019", JSON)
5853
if resp != c.expected {
5954
t.Errorf("got %q want %q", resp, c.expected)
6055
}

0 commit comments

Comments
 (0)