Skip to content

Commit bd9b551

Browse files
committed
feat: add version command output fields
1 parent 7a93fbf commit bd9b551

File tree

2 files changed

+143
-27
lines changed

2 files changed

+143
-27
lines changed

cmd/kops/version.go

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ package main
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"io"
2324

25+
"sigs.k8s.io/yaml"
26+
2427
"github.com/spf13/cobra"
2528
"k8s.io/kops"
2629
"k8s.io/kops/cmd/kops/util"
@@ -57,6 +60,7 @@ func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command {
5760

5861
cmd.Flags().BoolVar(&options.short, "short", options.short, "only print the main kOps version. Useful for scripting.")
5962
cmd.Flags().BoolVar(&options.server, "server", options.server, "show the kOps version that made the last change to the state store.")
63+
cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "One of 'yaml' or 'json'.")
6064

6165
return cmd
6266
}
@@ -65,43 +69,69 @@ type VersionOptions struct {
6569
short bool
6670
server bool
6771
ClusterName string
72+
Output string
73+
}
74+
75+
// Version is a struct for version information
76+
type Version struct {
77+
ClientVersion *kops.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
78+
ServerVersion string `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
6879
}
6980

7081
// RunVersion implements the version command logic
7182
func RunVersion(f *util.Factory, out io.Writer, options *VersionOptions) error {
72-
if options.short {
73-
s := kops.Version
74-
_, err := fmt.Fprintf(out, "%s\n", s)
75-
if err != nil {
76-
return err
77-
}
78-
if options.server {
79-
server := serverVersion(f, options)
80-
81-
_, err := fmt.Fprintf(out, "%s\n", server)
82-
return err
83-
}
84-
85-
return nil
86-
} else {
87-
client := kops.Version
88-
if kops.GitVersion != "" {
89-
client += " (git-" + kops.GitVersion + ")"
90-
}
91-
92-
{
93-
_, err := fmt.Fprintf(out, "Client version: %s\n", client)
83+
var versionInfo Version
84+
clientVersion := kops.Get()
85+
versionInfo.ClientVersion = &clientVersion
86+
if options.server {
87+
versionInfo.ServerVersion = serverVersion(f, options)
88+
}
89+
switch options.Output {
90+
case "":
91+
if options.short {
92+
_, err := fmt.Fprintf(out, "%s\n", versionInfo.ClientVersion.Version)
9493
if err != nil {
9594
return err
9695
}
97-
}
98-
if options.server {
99-
server := serverVersion(f, options)
96+
if options.server {
97+
_, err := fmt.Fprintf(out, "%s\n", versionInfo.ServerVersion)
98+
return err
99+
}
100+
return nil
101+
} else {
102+
client := versionInfo.ClientVersion.Version
103+
if versionInfo.ClientVersion.GitVersion != "" {
104+
client += " (git-" + versionInfo.ClientVersion.GitVersion + ")"
105+
}
100106

101-
_, err := fmt.Fprintf(out, "Last applied server version: %s\n", server)
107+
{
108+
_, err := fmt.Fprintf(out, "Client Version: %s\n", client)
109+
if err != nil {
110+
return err
111+
}
112+
}
113+
if options.server {
114+
_, err := fmt.Fprintf(out, "Last applied server version: %s\n", versionInfo.ServerVersion)
115+
return err
116+
}
117+
return nil
118+
}
119+
case OutputYaml:
120+
marshalled, err := yaml.Marshal(&versionInfo)
121+
if err != nil {
122+
return err
123+
}
124+
_, err = fmt.Fprintln(out, string(marshalled))
125+
return err
126+
case OutputJSON:
127+
marshalled, err := json.MarshalIndent(&versionInfo, "", " ")
128+
if err != nil {
102129
return err
103130
}
104-
return nil
131+
_, err = fmt.Fprintln(out, string(marshalled))
132+
return err
133+
default:
134+
return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", options.Output)
105135
}
106136
}
107137

cmd/kops/version_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
"k8s.io/kops/cmd/kops/util"
25+
"testing"
26+
)
27+
28+
func TestRunVersion(t *testing.T) {
29+
factoryOptions := &util.FactoryOptions{}
30+
factory := util.NewFactory(factoryOptions)
31+
32+
tests := []struct {
33+
name string
34+
opt *VersionOptions
35+
expectedOutput string
36+
wantErr error
37+
}{
38+
{
39+
name: "client Version",
40+
opt: &VersionOptions{
41+
Output: "",
42+
},
43+
expectedOutput: "Client Version",
44+
},
45+
{
46+
name: "output yaml format",
47+
opt: &VersionOptions{
48+
Output: OutputYaml,
49+
},
50+
expectedOutput: "clientVersion",
51+
},
52+
{
53+
name: "output json format",
54+
opt: &VersionOptions{
55+
Output: OutputJSON,
56+
},
57+
expectedOutput: "\"clientVersion\"",
58+
},
59+
{
60+
name: "unknown output format",
61+
opt: &VersionOptions{
62+
Output: "Xml",
63+
},
64+
wantErr: fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", "Xml"),
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
var stdout bytes.Buffer
70+
err := RunVersion(factory, &stdout, tt.opt)
71+
require.Equal(t, tt.wantErr, err)
72+
if err != nil {
73+
return
74+
}
75+
assert.Containsf(
76+
t,
77+
stdout.String(),
78+
tt.expectedOutput,
79+
"%s : Unexpected output! Expected\n%s\ngot\n%s",
80+
tt.name,
81+
tt.expectedOutput,
82+
stdout,
83+
)
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)