fix kubectl get --show-kind

pull/8/head
Cao Shufeng 2018-05-20 13:26:53 +08:00
parent 2a989c60ff
commit a1bba62202
4 changed files with 72 additions and 11 deletions

View File

@ -219,6 +219,9 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
o.IncludeUninitialized = cmdutil.ShouldIncludeUninitialized(cmd, false)
if resource.MultipleTypesRequested(args) {
o.PrintFlags.EnsureWithKind()
}
o.ToPrinter = func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) {
// make a new copy of current flags / opts before mutating
printFlags := o.PrintFlags.Copy()
@ -229,9 +232,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
printFlags.UseOpenAPIColumns(apiSchema, mapping)
}
}
if resource.MultipleTypesRequested(args) {
printFlags.EnsureWithKind(mapping.GroupVersionKind.GroupKind())
}
printFlags.SetKind(mapping.GroupVersionKind.GroupKind())
}
if withNamespace {
printFlags.EnsureWithNamespace()

View File

@ -41,6 +41,11 @@ type PrintFlags struct {
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
// a printer capable of printing with a "namespace" column.
func (f *PrintFlags) EnsureWithNamespace() error {
@ -49,8 +54,8 @@ func (f *PrintFlags) EnsureWithNamespace() error {
// EnsureWithKind ensures that humanreadable flags return
// a printer capable of including resource kinds.
func (f *PrintFlags) EnsureWithKind(kind schema.GroupKind) error {
return f.HumanReadableFlags.EnsureWithKind(kind)
func (f *PrintFlags) EnsureWithKind() error {
return f.HumanReadableFlags.EnsureWithKind()
}
// Copy returns a copy of PrintFlags for mutation

View File

@ -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) {
initTestErrorHandler(t)

View File

@ -43,13 +43,14 @@ type HumanPrintFlags struct {
WithNamespace bool
}
// EnsureWithKind sets the provided GroupKind humanreadable value.
// If the kind received is non-empty, the "showKind" humanreadable
// printer option is set to true.
func (f *HumanPrintFlags) EnsureWithKind(kind schema.GroupKind) error {
showKind := !kind.Empty()
// SetKind sets the Kind option
func (f *HumanPrintFlags) SetKind(kind schema.GroupKind) {
f.Kind = kind
}
// EnsureWithKind sets the "Showkind" humanreadable option to true.
func (f *HumanPrintFlags) EnsureWithKind() error {
showKind := true
f.ShowKind = &showKind
return nil
}