mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Merge pull request #114682 from brianpursley/get-cmd-arg
cmd/get: Remove cmd argument from Run()
This commit is contained in:
		@@ -538,7 +538,7 @@ func registerCompletionFuncForGlobalFlags(cmd *cobra.Command, f cmdutil.Factory)
 | 
				
			|||||||
	cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
						cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
				
			||||||
		"namespace",
 | 
							"namespace",
 | 
				
			||||||
		func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
							func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
			return utilcomp.CompGetResource(f, cmd, "namespace", toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
								return utilcomp.CompGetResource(f, "namespace", toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
		}))
 | 
							}))
 | 
				
			||||||
	cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
						cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
				
			||||||
		"context",
 | 
							"context",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -109,14 +109,14 @@ func NewCmdCp(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.C
 | 
				
			|||||||
					// complete <namespace>/<pod>
 | 
										// complete <namespace>/<pod>
 | 
				
			||||||
					namespace := toComplete[:idx]
 | 
										namespace := toComplete[:idx]
 | 
				
			||||||
					template := "{{ range .items }}{{ .metadata.namespace }}/{{ .metadata.name }}: {{ end }}"
 | 
										template := "{{ range .items }}{{ .metadata.namespace }}/{{ .metadata.name }}: {{ end }}"
 | 
				
			||||||
					comps = completion.CompGetFromTemplate(&template, f, namespace, cmd, []string{"pod"}, toComplete)
 | 
										comps = completion.CompGetFromTemplate(&template, f, namespace, []string{"pod"}, toComplete)
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					// Complete namespaces followed by a /
 | 
										// Complete namespaces followed by a /
 | 
				
			||||||
					for _, ns := range completion.CompGetResource(f, cmd, "namespace", toComplete) {
 | 
										for _, ns := range completion.CompGetResource(f, "namespace", toComplete) {
 | 
				
			||||||
						comps = append(comps, fmt.Sprintf("%s/", ns))
 | 
											comps = append(comps, fmt.Sprintf("%s/", ns))
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					// Complete pod names followed by a :
 | 
										// Complete pod names followed by a :
 | 
				
			||||||
					for _, pod := range completion.CompGetResource(f, cmd, "pod", toComplete) {
 | 
										for _, pod := range completion.CompGetResource(f, "pod", toComplete) {
 | 
				
			||||||
						comps = append(comps, fmt.Sprintf("%s:", pod))
 | 
											comps = append(comps, fmt.Sprintf("%s:", pod))
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -108,7 +108,7 @@ func NewCmdCreateClusterRoleBinding(f cmdutil.Factory, ioStreams genericclioptio
 | 
				
			|||||||
	cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
						cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
 | 
				
			||||||
		"clusterrole",
 | 
							"clusterrole",
 | 
				
			||||||
		func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
							func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
			return completion.CompGetResource(f, cmd, "clusterrole", toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
								return completion.CompGetResource(f, "clusterrole", toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
		}))
 | 
							}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return cmd
 | 
						return cmd
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -75,11 +75,11 @@ type GetOptions struct {
 | 
				
			|||||||
	Namespace         string
 | 
						Namespace         string
 | 
				
			||||||
	ExplicitNamespace bool
 | 
						ExplicitNamespace bool
 | 
				
			||||||
	Subresource       string
 | 
						Subresource       string
 | 
				
			||||||
 | 
						SortBy            string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ServerPrint bool
 | 
						ServerPrint bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	NoHeaders      bool
 | 
						NoHeaders      bool
 | 
				
			||||||
	Sort           bool
 | 
					 | 
				
			||||||
	IgnoreNotFound bool
 | 
						IgnoreNotFound bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	genericclioptions.IOStreams
 | 
						genericclioptions.IOStreams
 | 
				
			||||||
@@ -168,7 +168,7 @@ func NewCmdGet(parent string, f cmdutil.Factory, streams genericclioptions.IOStr
 | 
				
			|||||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
							Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
			cmdutil.CheckErr(o.Complete(f, cmd, args))
 | 
								cmdutil.CheckErr(o.Complete(f, cmd, args))
 | 
				
			||||||
			cmdutil.CheckErr(o.Validate())
 | 
								cmdutil.CheckErr(o.Validate())
 | 
				
			||||||
			cmdutil.CheckErr(o.Run(f, cmd, args))
 | 
								cmdutil.CheckErr(o.Run(f, args))
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		SuggestFor: []string{"list", "ps"},
 | 
							SuggestFor: []string{"list", "ps"},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -208,11 +208,9 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
 | 
				
			|||||||
		o.ExplicitNamespace = false
 | 
							o.ExplicitNamespace = false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sortBy, err := cmd.Flags().GetString("sort-by")
 | 
						if o.PrintFlags.HumanReadableFlags.SortBy != nil {
 | 
				
			||||||
	if err != nil {
 | 
							o.SortBy = *o.PrintFlags.HumanReadableFlags.SortBy
 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	o.Sort = len(sortBy) > 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	o.NoHeaders = cmdutil.GetFlagBool(cmd, "no-headers")
 | 
						o.NoHeaders = cmdutil.GetFlagBool(cmd, "no-headers")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -256,8 +254,8 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
 | 
				
			|||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if o.Sort {
 | 
							if len(o.SortBy) > 0 {
 | 
				
			||||||
			printer = &SortingPrinter{Delegate: printer, SortField: sortBy}
 | 
								printer = &SortingPrinter{Delegate: printer, SortField: o.SortBy}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if outputObjects != nil {
 | 
							if outputObjects != nil {
 | 
				
			||||||
			printer = &skipPrinter{delegate: printer, output: outputObjects}
 | 
								printer = &skipPrinter{delegate: printer, output: outputObjects}
 | 
				
			||||||
@@ -270,7 +268,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	switch {
 | 
						switch {
 | 
				
			||||||
	case o.Watch || o.WatchOnly:
 | 
						case o.Watch || o.WatchOnly:
 | 
				
			||||||
		if o.Sort {
 | 
							if len(o.SortBy) > 0 {
 | 
				
			||||||
			fmt.Fprintf(o.IOStreams.ErrOut, "warning: --watch or --watch-only requested, --sort-by will be ignored\n")
 | 
								fmt.Fprintf(o.IOStreams.ErrOut, "warning: --watch or --watch-only requested, --sort-by will be ignored\n")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
@@ -431,14 +429,14 @@ func (o *GetOptions) transformRequests(req *rest.Request) {
 | 
				
			|||||||
	}, ","))
 | 
						}, ","))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// if sorting, ensure we receive the full object in order to introspect its fields via jsonpath
 | 
						// if sorting, ensure we receive the full object in order to introspect its fields via jsonpath
 | 
				
			||||||
	if o.Sort {
 | 
						if len(o.SortBy) > 0 {
 | 
				
			||||||
		req.Param("includeObject", "Object")
 | 
							req.Param("includeObject", "Object")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Run performs the get operation.
 | 
					// Run performs the get operation.
 | 
				
			||||||
// TODO: remove the need to pass these arguments, like other commands.
 | 
					// TODO: remove the need to pass these arguments, like other commands.
 | 
				
			||||||
func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
 | 
					func (o *GetOptions) Run(f cmdutil.Factory, args []string) error {
 | 
				
			||||||
	if len(o.Raw) > 0 {
 | 
						if len(o.Raw) > 0 {
 | 
				
			||||||
		restClient, err := f.RESTClient()
 | 
							restClient, err := f.RESTClient()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
@@ -447,11 +445,11 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
 | 
				
			|||||||
		return rawhttp.RawGet(restClient, o.IOStreams, o.Raw)
 | 
							return rawhttp.RawGet(restClient, o.IOStreams, o.Raw)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if o.Watch || o.WatchOnly {
 | 
						if o.Watch || o.WatchOnly {
 | 
				
			||||||
		return o.watch(f, cmd, args)
 | 
							return o.watch(f, args)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	chunkSize := o.ChunkSize
 | 
						chunkSize := o.ChunkSize
 | 
				
			||||||
	if o.Sort {
 | 
						if len(o.SortBy) > 0 {
 | 
				
			||||||
		// TODO(juanvallejo): in the future, we could have the client use chunking
 | 
							// TODO(juanvallejo): in the future, we could have the client use chunking
 | 
				
			||||||
		// to gather all results, then sort them all at the end to reduce server load.
 | 
							// to gather all results, then sort them all at the end to reduce server load.
 | 
				
			||||||
		chunkSize = 0
 | 
							chunkSize = 0
 | 
				
			||||||
@@ -496,14 +494,9 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
 | 
				
			|||||||
		objs[ix] = infos[ix].Object
 | 
							objs[ix] = infos[ix].Object
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sorting, err := cmd.Flags().GetString("sort-by")
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var positioner OriginalPositioner
 | 
						var positioner OriginalPositioner
 | 
				
			||||||
	if o.Sort {
 | 
						if len(o.SortBy) > 0 {
 | 
				
			||||||
		sorter := NewRuntimeSorter(objs, sorting)
 | 
							sorter := NewRuntimeSorter(objs, o.SortBy)
 | 
				
			||||||
		if err := sorter.Sort(); err != nil {
 | 
							if err := sorter.Sort(); err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -608,7 +601,7 @@ func (s *separatorWriterWrapper) SetReady(state bool) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// watch starts a client-side watch of one or more resources.
 | 
					// watch starts a client-side watch of one or more resources.
 | 
				
			||||||
// TODO: remove the need for arguments here.
 | 
					// TODO: remove the need for arguments here.
 | 
				
			||||||
func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
 | 
					func (o *GetOptions) watch(f cmdutil.Factory, args []string) error {
 | 
				
			||||||
	r := f.NewBuilder().
 | 
						r := f.NewBuilder().
 | 
				
			||||||
		Unstructured().
 | 
							Unstructured().
 | 
				
			||||||
		NamespaceParam(o.Namespace).DefaultNamespace().AllNamespaces(o.AllNamespaces).
 | 
							NamespaceParam(o.Namespace).DefaultNamespace().AllNamespaces(o.AllNamespaces).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,6 +25,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
						"k8s.io/apimachinery/pkg/api/meta"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
						"k8s.io/cli-runtime/pkg/genericclioptions"
 | 
				
			||||||
	"k8s.io/cli-runtime/pkg/printers"
 | 
						"k8s.io/cli-runtime/pkg/printers"
 | 
				
			||||||
@@ -69,10 +70,10 @@ func SpecifiedResourceTypeAndNameNoRepeatCompletionFunc(f cmdutil.Factory, allow
 | 
				
			|||||||
// that don't support that form.  For commands that apply to pods and that support the <type>/<name>
 | 
					// that don't support that form.  For commands that apply to pods and that support the <type>/<name>
 | 
				
			||||||
// form, please use PodResourceNameCompletionFunc()
 | 
					// form, please use PodResourceNameCompletionFunc()
 | 
				
			||||||
func ResourceNameCompletionFunc(f cmdutil.Factory, resourceType string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
					func ResourceNameCompletionFunc(f cmdutil.Factory, resourceType string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
						return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
		var comps []string
 | 
							var comps []string
 | 
				
			||||||
		if len(args) == 0 {
 | 
							if len(args) == 0 {
 | 
				
			||||||
			comps = CompGetResource(f, cmd, resourceType, toComplete)
 | 
								comps = CompGetResource(f, resourceType, toComplete)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return comps, cobra.ShellCompDirectiveNoFileComp
 | 
							return comps, cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -82,11 +83,11 @@ func ResourceNameCompletionFunc(f cmdutil.Factory, resourceType string) func(*co
 | 
				
			|||||||
// 1- pod names that match the toComplete prefix
 | 
					// 1- pod names that match the toComplete prefix
 | 
				
			||||||
// 2- resource types containing pods which match the toComplete prefix
 | 
					// 2- resource types containing pods which match the toComplete prefix
 | 
				
			||||||
func PodResourceNameCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
					func PodResourceNameCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
						return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
		var comps []string
 | 
							var comps []string
 | 
				
			||||||
		directive := cobra.ShellCompDirectiveNoFileComp
 | 
							directive := cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
		if len(args) == 0 {
 | 
							if len(args) == 0 {
 | 
				
			||||||
			comps, directive = doPodResourceCompletion(f, cmd, toComplete)
 | 
								comps, directive = doPodResourceCompletion(f, toComplete)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return comps, directive
 | 
							return comps, directive
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -97,14 +98,14 @@ func PodResourceNameCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []str
 | 
				
			|||||||
// 2- resource types containing pods which match the toComplete prefix
 | 
					// 2- resource types containing pods which match the toComplete prefix
 | 
				
			||||||
// and as a second argument the containers within the specified pod.
 | 
					// and as a second argument the containers within the specified pod.
 | 
				
			||||||
func PodResourceNameAndContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
					func PodResourceNameAndContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
						return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
		var comps []string
 | 
							var comps []string
 | 
				
			||||||
		directive := cobra.ShellCompDirectiveNoFileComp
 | 
							directive := cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
		if len(args) == 0 {
 | 
							if len(args) == 0 {
 | 
				
			||||||
			comps, directive = doPodResourceCompletion(f, cmd, toComplete)
 | 
								comps, directive = doPodResourceCompletion(f, toComplete)
 | 
				
			||||||
		} else if len(args) == 1 {
 | 
							} else if len(args) == 1 {
 | 
				
			||||||
			podName := convertResourceNameToPodName(f, args[0])
 | 
								podName := convertResourceNameToPodName(f, args[0])
 | 
				
			||||||
			comps = CompGetContainers(f, cmd, podName, toComplete)
 | 
								comps = CompGetContainers(f, podName, toComplete)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return comps, directive
 | 
							return comps, directive
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -114,13 +115,13 @@ func PodResourceNameAndContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Co
 | 
				
			|||||||
// pod specified by the first argument.  The resource containing the pod can be specified in
 | 
					// pod specified by the first argument.  The resource containing the pod can be specified in
 | 
				
			||||||
// the <type>/<name> form.
 | 
					// the <type>/<name> form.
 | 
				
			||||||
func ContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
					func ContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
						return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
		var comps []string
 | 
							var comps []string
 | 
				
			||||||
		// We need the pod name to be able to complete the container names, it must be in args[0].
 | 
							// We need the pod name to be able to complete the container names, it must be in args[0].
 | 
				
			||||||
		// That first argument can also be of the form <type>/<name> so we need to convert it.
 | 
							// That first argument can also be of the form <type>/<name> so we need to convert it.
 | 
				
			||||||
		if len(args) > 0 {
 | 
							if len(args) > 0 {
 | 
				
			||||||
			podName := convertResourceNameToPodName(f, args[0])
 | 
								podName := convertResourceNameToPodName(f, args[0])
 | 
				
			||||||
			comps = CompGetContainers(f, cmd, podName, toComplete)
 | 
								comps = CompGetContainers(f, podName, toComplete)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return comps, cobra.ShellCompDirectiveNoFileComp
 | 
							return comps, cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -128,7 +129,7 @@ func ContainerCompletionFunc(f cmdutil.Factory) func(*cobra.Command, []string, s
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ContextCompletionFunc is a completion function that completes as a first argument the
 | 
					// ContextCompletionFunc is a completion function that completes as a first argument the
 | 
				
			||||||
// context names that match the toComplete prefix
 | 
					// context names that match the toComplete prefix
 | 
				
			||||||
func ContextCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
					func ContextCompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	if len(args) == 0 {
 | 
						if len(args) == 0 {
 | 
				
			||||||
		return ListContextsInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
							return ListContextsInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -137,7 +138,7 @@ func ContextCompletionFunc(cmd *cobra.Command, args []string, toComplete string)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ClusterCompletionFunc is a completion function that completes as a first argument the
 | 
					// ClusterCompletionFunc is a completion function that completes as a first argument the
 | 
				
			||||||
// cluster names that match the toComplete prefix
 | 
					// cluster names that match the toComplete prefix
 | 
				
			||||||
func ClusterCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
					func ClusterCompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	if len(args) == 0 {
 | 
						if len(args) == 0 {
 | 
				
			||||||
		return ListClustersInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
							return ListClustersInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -146,7 +147,7 @@ func ClusterCompletionFunc(cmd *cobra.Command, args []string, toComplete string)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// UserCompletionFunc is a completion function that completes as a first argument the
 | 
					// UserCompletionFunc is a completion function that completes as a first argument the
 | 
				
			||||||
// user names that match the toComplete prefix
 | 
					// user names that match the toComplete prefix
 | 
				
			||||||
func UserCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
					func UserCompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	if len(args) == 0 {
 | 
						if len(args) == 0 {
 | 
				
			||||||
		return ListUsersInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
							return ListUsersInConfig(toComplete), cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -154,20 +155,20 @@ func UserCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CompGetResource gets the list of the resource specified which begin with `toComplete`.
 | 
					// CompGetResource gets the list of the resource specified which begin with `toComplete`.
 | 
				
			||||||
func CompGetResource(f cmdutil.Factory, cmd *cobra.Command, resourceName string, toComplete string) []string {
 | 
					func CompGetResource(f cmdutil.Factory, resourceName string, toComplete string) []string {
 | 
				
			||||||
	template := "{{ range .items  }}{{ .metadata.name }} {{ end }}"
 | 
						template := "{{ range .items  }}{{ .metadata.name }} {{ end }}"
 | 
				
			||||||
	return CompGetFromTemplate(&template, f, "", cmd, []string{resourceName}, toComplete)
 | 
						return CompGetFromTemplate(&template, f, "", []string{resourceName}, toComplete)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CompGetContainers gets the list of containers of the specified pod which begin with `toComplete`.
 | 
					// CompGetContainers gets the list of containers of the specified pod which begin with `toComplete`.
 | 
				
			||||||
func CompGetContainers(f cmdutil.Factory, cmd *cobra.Command, podName string, toComplete string) []string {
 | 
					func CompGetContainers(f cmdutil.Factory, podName string, toComplete string) []string {
 | 
				
			||||||
	template := "{{ range .spec.initContainers }}{{ .name }} {{end}}{{ range .spec.containers  }}{{ .name }} {{ end }}"
 | 
						template := "{{ range .spec.initContainers }}{{ .name }} {{end}}{{ range .spec.containers  }}{{ .name }} {{ end }}"
 | 
				
			||||||
	return CompGetFromTemplate(&template, f, "", cmd, []string{"pod", podName}, toComplete)
 | 
						return CompGetFromTemplate(&template, f, "", []string{"pod", podName}, toComplete)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CompGetFromTemplate executes a Get operation using the specified template and args and returns the results
 | 
					// CompGetFromTemplate executes a Get operation using the specified template and args and returns the results
 | 
				
			||||||
// which begin with `toComplete`.
 | 
					// which begin with `toComplete`.
 | 
				
			||||||
func CompGetFromTemplate(template *string, f cmdutil.Factory, namespace string, cmd *cobra.Command, args []string, toComplete string) []string {
 | 
					func CompGetFromTemplate(template *string, f cmdutil.Factory, namespace string, args []string, toComplete string) []string {
 | 
				
			||||||
	buf := new(bytes.Buffer)
 | 
						buf := new(bytes.Buffer)
 | 
				
			||||||
	streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: io.Discard}
 | 
						streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: io.Discard}
 | 
				
			||||||
	o := get.NewGetOptions("kubectl", streams)
 | 
						o := get.NewGetOptions("kubectl", streams)
 | 
				
			||||||
@@ -199,7 +200,7 @@ func CompGetFromTemplate(template *string, f cmdutil.Factory, namespace string,
 | 
				
			|||||||
		return printer.PrintObj, nil
 | 
							return printer.PrintObj, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	o.Run(f, cmd, args)
 | 
						o.Run(f, args)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var comps []string
 | 
						var comps []string
 | 
				
			||||||
	resources := strings.Split(buf.String(), " ")
 | 
						resources := strings.Split(buf.String(), " ")
 | 
				
			||||||
@@ -304,7 +305,7 @@ func resourceTypeAndNameCompletionFunc(f cmdutil.Factory, allowedTypes []string,
 | 
				
			|||||||
			// The first argument is of the form <type> (e.g., pods)
 | 
								// The first argument is of the form <type> (e.g., pods)
 | 
				
			||||||
			// All following arguments should be a resource name.
 | 
								// All following arguments should be a resource name.
 | 
				
			||||||
			if allowRepeat || len(args) == 1 {
 | 
								if allowRepeat || len(args) == 1 {
 | 
				
			||||||
				comps = CompGetResource(f, cmd, args[0], toComplete)
 | 
									comps = CompGetResource(f, args[0], toComplete)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Remove choices already on the command-line
 | 
									// Remove choices already on the command-line
 | 
				
			||||||
				if len(args) > 1 {
 | 
									if len(args) > 1 {
 | 
				
			||||||
@@ -356,7 +357,7 @@ func resourceTypeAndNameCompletionFunc(f cmdutil.Factory, allowedTypes []string,
 | 
				
			|||||||
				if allowRepeat || len(args) == 0 {
 | 
									if allowRepeat || len(args) == 0 {
 | 
				
			||||||
					resourceType := toComplete[:slashIdx]
 | 
										resourceType := toComplete[:slashIdx]
 | 
				
			||||||
					toComplete = toComplete[slashIdx+1:]
 | 
										toComplete = toComplete[slashIdx+1:]
 | 
				
			||||||
					nameComps := CompGetResource(f, cmd, resourceType, toComplete)
 | 
										nameComps := CompGetResource(f, resourceType, toComplete)
 | 
				
			||||||
					for _, c := range nameComps {
 | 
										for _, c := range nameComps {
 | 
				
			||||||
						comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
 | 
											comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
@@ -375,13 +376,13 @@ func resourceTypeAndNameCompletionFunc(f cmdutil.Factory, allowedTypes []string,
 | 
				
			|||||||
// doPodResourceCompletion Returns completions of:
 | 
					// doPodResourceCompletion Returns completions of:
 | 
				
			||||||
// 1- pod names that match the toComplete prefix
 | 
					// 1- pod names that match the toComplete prefix
 | 
				
			||||||
// 2- resource types containing pods which match the toComplete prefix
 | 
					// 2- resource types containing pods which match the toComplete prefix
 | 
				
			||||||
func doPodResourceCompletion(f cmdutil.Factory, cmd *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
					func doPodResourceCompletion(f cmdutil.Factory, toComplete string) ([]string, cobra.ShellCompDirective) {
 | 
				
			||||||
	var comps []string
 | 
						var comps []string
 | 
				
			||||||
	directive := cobra.ShellCompDirectiveNoFileComp
 | 
						directive := cobra.ShellCompDirectiveNoFileComp
 | 
				
			||||||
	slashIdx := strings.Index(toComplete, "/")
 | 
						slashIdx := strings.Index(toComplete, "/")
 | 
				
			||||||
	if slashIdx == -1 {
 | 
						if slashIdx == -1 {
 | 
				
			||||||
		// Standard case, complete pod names
 | 
							// Standard case, complete pod names
 | 
				
			||||||
		comps = CompGetResource(f, cmd, "pod", toComplete)
 | 
							comps = CompGetResource(f, "pod", toComplete)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Also include resource choices for the <type>/<name> form,
 | 
							// Also include resource choices for the <type>/<name> form,
 | 
				
			||||||
		// but only for resources that contain pods
 | 
							// but only for resources that contain pods
 | 
				
			||||||
@@ -410,7 +411,7 @@ func doPodResourceCompletion(f cmdutil.Factory, cmd *cobra.Command, toComplete s
 | 
				
			|||||||
		// Dealing with the <type>/<name> form, use the specified resource type
 | 
							// Dealing with the <type>/<name> form, use the specified resource type
 | 
				
			||||||
		resourceType := toComplete[:slashIdx]
 | 
							resourceType := toComplete[:slashIdx]
 | 
				
			||||||
		toComplete = toComplete[slashIdx+1:]
 | 
							toComplete = toComplete[slashIdx+1:]
 | 
				
			||||||
		nameComps := CompGetResource(f, cmd, resourceType, toComplete)
 | 
							nameComps := CompGetResource(f, resourceType, toComplete)
 | 
				
			||||||
		for _, c := range nameComps {
 | 
							for _, c := range nameComps {
 | 
				
			||||||
			comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
 | 
								comps = append(comps, fmt.Sprintf("%s/%s", resourceType, c))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user