Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions staging/src/k8s.io/kubectl/pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewCmdVersion(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cob
},
}
cmd.Flags().BoolVar(&o.ClientOnly, "client", o.ClientOnly, "If true, shows client version only (no server required).")
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml', 'json', or 'short'.")
return cmd
}

Expand Down Expand Up @@ -117,8 +117,8 @@ func (o *Options) Validate() error {
return errors.New(fmt.Sprintf("extra arguments: %v", o.args))
}

if o.Output != "" && o.Output != "yaml" && o.Output != "json" {
return errors.New(`--output must be 'yaml' or 'json'`)
if o.Output != "" && o.Output != "yaml" && o.Output != "json" && o.Output != "short" {
return errors.New(`--output must be 'yaml', 'json', or 'short'`)
}

return nil
Expand Down Expand Up @@ -147,6 +147,11 @@ func (o *Options) Run() error {
if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "Server Version: %s\n", versionInfo.ServerVersion.GitVersion)
}
case "short":
fmt.Fprintf(o.Out, "%s\n", versionInfo.ClientVersion.GitVersion)
if versionInfo.ServerVersion != nil {
fmt.Fprintf(o.Out, "%s\n", versionInfo.ServerVersion.GitVersion)
}
case "yaml":
marshalled, err := yaml.Marshal(&versionInfo)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,37 @@ func TestNewCmdVersionClientVersion(t *testing.T) {
t.Errorf("unexpected output: %s", buf.String())
}
}

func TestNewCmdVersionShortOutput(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
streams, _, buf, _ := genericiooptions.NewTestIOStreams()
o := NewOptions(streams)
o.ClientOnly = true
o.Output = "short"

cmd := NewCmdVersion(tf, streams)
cmd.Flags().Bool("warnings-as-errors", false, "")

if err := o.Complete(tf, cmd, nil); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err := o.Validate(); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err := o.Run(); err != nil {
t.Errorf("Cannot execute version command: %v", err)
}

output := buf.String()
lines := strings.Split(strings.TrimSpace(output), "\n")
if len(lines) != 1 {
t.Errorf("Expected 1 line of output for client-only short format, got %d: %s", len(lines), output)
}
if !strings.HasPrefix(lines[0], "v") {
t.Errorf("Expected version string to start with 'v', got: %s", lines[0])
}
if strings.Contains(output, "Client Version:") || strings.Contains(output, "Server Version:") {
t.Errorf("Short output should not contain labels, got: %s", output)
}
}