diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/version/version.go b/staging/src/k8s.io/kubectl/pkg/cmd/version/version.go index 7ee3e3f864868..526da7e275126 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/version/version.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/version/version.go @@ -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 } @@ -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 @@ -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 { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go index 606a1af331e29..3cfae379ddc0a 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go @@ -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) + } +}