Commit a1bba622 authored by Cao Shufeng's avatar Cao Shufeng

fix kubectl get --show-kind

parent 2a989c60
...@@ -219,6 +219,9 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri ...@@ -219,6 +219,9 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
o.IncludeUninitialized = cmdutil.ShouldIncludeUninitialized(cmd, false) o.IncludeUninitialized = cmdutil.ShouldIncludeUninitialized(cmd, false)
if resource.MultipleTypesRequested(args) {
o.PrintFlags.EnsureWithKind()
}
o.ToPrinter = func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) { o.ToPrinter = func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) {
// make a new copy of current flags / opts before mutating // make a new copy of current flags / opts before mutating
printFlags := o.PrintFlags.Copy() printFlags := o.PrintFlags.Copy()
...@@ -229,9 +232,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri ...@@ -229,9 +232,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
printFlags.UseOpenAPIColumns(apiSchema, mapping) printFlags.UseOpenAPIColumns(apiSchema, mapping)
} }
} }
if resource.MultipleTypesRequested(args) { printFlags.SetKind(mapping.GroupVersionKind.GroupKind())
printFlags.EnsureWithKind(mapping.GroupVersionKind.GroupKind())
}
} }
if withNamespace { if withNamespace {
printFlags.EnsureWithNamespace() printFlags.EnsureWithNamespace()
......
...@@ -41,6 +41,11 @@ type PrintFlags struct { ...@@ -41,6 +41,11 @@ type PrintFlags struct {
OutputFormat *string OutputFormat *string
} }
// SetKind sets the Kind option of humanreadable flags
func (f *PrintFlags) SetKind(kind schema.GroupKind) {
f.HumanReadableFlags.SetKind(kind)
}
// EnsureWithNamespace ensures that humanreadable flags return // EnsureWithNamespace ensures that humanreadable flags return
// a printer capable of printing with a "namespace" column. // a printer capable of printing with a "namespace" column.
func (f *PrintFlags) EnsureWithNamespace() error { func (f *PrintFlags) EnsureWithNamespace() error {
...@@ -49,8 +54,8 @@ func (f *PrintFlags) EnsureWithNamespace() error { ...@@ -49,8 +54,8 @@ func (f *PrintFlags) EnsureWithNamespace() error {
// EnsureWithKind ensures that humanreadable flags return // EnsureWithKind ensures that humanreadable flags return
// a printer capable of including resource kinds. // a printer capable of including resource kinds.
func (f *PrintFlags) EnsureWithKind(kind schema.GroupKind) error { func (f *PrintFlags) EnsureWithKind() error {
return f.HumanReadableFlags.EnsureWithKind(kind) return f.HumanReadableFlags.EnsureWithKind()
} }
// Copy returns a copy of PrintFlags for mutation // Copy returns a copy of PrintFlags for mutation
......
...@@ -329,6 +329,60 @@ foo 0/0 0 <unknown> ...@@ -329,6 +329,60 @@ foo 0/0 0 <unknown>
} }
} }
func TestGetObjectsShowKind(t *testing.T) {
pods, _, _ := testData()
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()
codec := legacyscheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
}
tf.Namespace = "test"
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
cmd.SetOutput(buf)
cmd.Flags().Set("show-kind", "true")
cmd.Run(cmd, []string{"pods", "foo"})
expected := `NAME READY STATUS RESTARTS AGE
pod/foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
func TestGetObjectsShowLabels(t *testing.T) {
pods, _, _ := testData()
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()
codec := legacyscheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
}
tf.Namespace = "test"
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
cmd.SetOutput(buf)
cmd.Flags().Set("show-labels", "true")
cmd.Run(cmd, []string{"pods", "foo"})
expected := `NAME READY STATUS RESTARTS AGE LABELS
foo 0/0 0 <unknown> <none>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
func TestGetObjectIgnoreNotFound(t *testing.T) { func TestGetObjectIgnoreNotFound(t *testing.T) {
initTestErrorHandler(t) initTestErrorHandler(t)
......
...@@ -43,13 +43,14 @@ type HumanPrintFlags struct { ...@@ -43,13 +43,14 @@ type HumanPrintFlags struct {
WithNamespace bool WithNamespace bool
} }
// EnsureWithKind sets the provided GroupKind humanreadable value. // SetKind sets the Kind option
// If the kind received is non-empty, the "showKind" humanreadable func (f *HumanPrintFlags) SetKind(kind schema.GroupKind) {
// printer option is set to true.
func (f *HumanPrintFlags) EnsureWithKind(kind schema.GroupKind) error {
showKind := !kind.Empty()
f.Kind = kind f.Kind = kind
}
// EnsureWithKind sets the "Showkind" humanreadable option to true.
func (f *HumanPrintFlags) EnsureWithKind() error {
showKind := true
f.ShowKind = &showKind f.ShowKind = &showKind
return nil return nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment