MCPcopy
hub / github.com/helm/helm / pluginDynamicComp

Function pluginDynamicComp

pkg/cmd/load_plugins.go:335–404  ·  view source on GitHub ↗

pluginDynamicComp call the plugin.complete script of the plugin (if available) to obtain the dynamic completion choices. It must pass all the flags and sub-commands specified in the command-line to the plugin.complete executable (except helm's global flags)

(plug plugin.Plugin, cmd *cobra.Command, args []string, toComplete string)

Source from the content-addressed store, hash-verified

333// to obtain the dynamic completion choices. It must pass all the flags and sub-commands
334// specified in the command-line to the plugin.complete executable (except helm's global flags)
335func pluginDynamicComp(plug plugin.Plugin, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
336
337 subprocessPlug, ok := plug.(*plugin.SubprocessPluginRuntime)
338 if !ok {
339 // Completion only supported for subprocess plugins (TODO: fix this)
340 cobra.CompDebugln(fmt.Sprintf("Unsupported plugin runtime: %q", plug.Metadata().Runtime), settings.Debug)
341 return nil, cobra.ShellCompDirectiveDefault
342 }
343
344 var ignoreFlags bool
345 if cliConfig, ok := subprocessPlug.Metadata().Config.(*schema.ConfigCLIV1); ok {
346 ignoreFlags = cliConfig.IgnoreFlags
347 }
348
349 u, err := processParent(cmd, args)
350 if err != nil {
351 return nil, cobra.ShellCompDirectiveError
352 }
353
354 // We will call the dynamic completion script of the plugin
355 main := strings.Join([]string{plug.Dir(), pluginDynamicCompletionExecutable}, string(filepath.Separator))
356
357 // We must include all sub-commands passed on the command-line.
358 // To do that, we pass-in the entire CommandPath, except the first two elements
359 // which are 'helm' and 'pluginName'.
360 argv := strings.Split(cmd.CommandPath(), " ")[2:]
361 if !ignoreFlags {
362 argv = append(argv, u...)
363 argv = append(argv, toComplete)
364 }
365
366 cobra.CompDebugln(fmt.Sprintf("calling %s with args %v", main, argv), settings.Debug)
367 buf := new(bytes.Buffer)
368
369 // Prepare environment
370 env := os.Environ()
371 for k, v := range settings.EnvVars() {
372 env = append(env, fmt.Sprintf("%s=%s", k, v))
373 }
374
375 // For subprocess runtime, use InvokeWithEnv for dynamic completion
376 if err := subprocessPlug.InvokeWithEnv(main, argv, env, nil, buf, buf); err != nil {
377 // The dynamic completion file is optional for a plugin, so this error is ok.
378 cobra.CompDebugln(fmt.Sprintf("Unable to call %s: %v", main, err.Error()), settings.Debug)
379 return nil, cobra.ShellCompDirectiveDefault
380 }
381
382 var completions []string
383 for comp := range strings.SplitSeq(buf.String(), "\n") {
384 // Remove any empty lines
385 if len(comp) > 0 {
386 completions = append(completions, comp)
387 }
388 }
389
390 // Check if the last line of output is of the form :<integer>, which
391 // indicates the BashCompletionDirective.
392 directive := cobra.ShellCompDirectiveDefault

Callers 1

addPluginCommandsFunction · 0.85

Calls 7

processParentFunction · 0.85
EnvVarsMethod · 0.80
InvokeWithEnvMethod · 0.80
MetadataMethod · 0.65
DirMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…