From 59ba00f82237bd78f92f4f8edef441418d6a767e Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Wed, 7 Sep 2016 15:33:26 -0400 Subject: [PATCH] print resource kind prefix when `kubectl get all` has single type to display This patch forces the HumanReadablePrinter to display resource kind prefixes when there is only one type of resource to show and a specific resource type has not been specified as an argument to kubectl get `$ kubectl get all` ``` NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 172.30.0.1 443/TCP,53/UDP,53/TCP 2m ``` `$ kubectl get all` ``` NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 172.30.0.1 443/TCP,53/UDP,53/TCP 2m ``` --- pkg/kubectl/cmd/get.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 159322f01e1..452097120c7 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -156,6 +156,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") showKind := cmdutil.GetFlagBool(cmd, "show-kind") mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd)) + printAll := false cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { @@ -171,6 +172,14 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm return cmdutil.UsageError(cmd, "Required resource not specified.") } + // determine if args contains "all" + for _, a := range args { + if a == "all" { + printAll = true + break + } + } + // always show resources when getting by name or filename argsHasNames, err := resource.HasNames(args) if err != nil { @@ -365,7 +374,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm var lastMapping *meta.RESTMapping w := kubectl.GetNewTabWriter(out) - if mustPrintWithKinds(objs, infos, sorter) { + if mustPrintWithKinds(objs, infos, sorter, printAll) { showKind = true } @@ -431,9 +440,13 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm // with multiple resource kinds, in which case it will // return true, indicating resource kind will be // included as part of printer output -func mustPrintWithKinds(objs []runtime.Object, infos []*resource.Info, sorter *kubectl.RuntimeSort) bool { +func mustPrintWithKinds(objs []runtime.Object, infos []*resource.Info, sorter *kubectl.RuntimeSort, printAll bool) bool { var lastMap *meta.RESTMapping + if len(infos) == 1 && printAll { + return true + } + for ix := range objs { var mapping *meta.RESTMapping if sorter != nil {