loadCLIPlugins loads CLI plugins into the command list. This follows a different pattern than the other commands because it has to inspect its environment and then add commands to the base command as it finds them.
(baseCmd *cobra.Command, out io.Writer)
| 53 | // to inspect its environment and then add commands to the base command |
| 54 | // as it finds them. |
| 55 | func loadCLIPlugins(baseCmd *cobra.Command, out io.Writer) { |
| 56 | // If HELM_NO_PLUGINS is set to 1, do not load plugins. |
| 57 | if os.Getenv("HELM_NO_PLUGINS") == "1" { |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | dirs := filepath.SplitList(settings.PluginsDirectory) |
| 62 | descriptor := plugin.Descriptor{ |
| 63 | Type: "cli/v1", |
| 64 | } |
| 65 | found, err := plugin.FindPlugins(dirs, descriptor) |
| 66 | if err != nil { |
| 67 | slog.Error("failed to load plugins", slog.String("error", err.Error())) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | // Now we create commands for all of these. |
| 72 | for _, plug := range found { |
| 73 | var use, short, long string |
| 74 | var ignoreFlags bool |
| 75 | if cliConfig, ok := plug.Metadata().Config.(*schema.ConfigCLIV1); ok { |
| 76 | use = cliConfig.Usage |
| 77 | short = cliConfig.ShortHelp |
| 78 | long = cliConfig.LongHelp |
| 79 | ignoreFlags = cliConfig.IgnoreFlags |
| 80 | } |
| 81 | |
| 82 | // Set defaults |
| 83 | if use == "" { |
| 84 | use = plug.Metadata().Name |
| 85 | } |
| 86 | if short == "" { |
| 87 | short = fmt.Sprintf("the %q plugin", plug.Metadata().Name) |
| 88 | } |
| 89 | // long has no default, empty is ok |
| 90 | |
| 91 | c := &cobra.Command{ |
| 92 | Use: use, |
| 93 | Short: short, |
| 94 | Long: long, |
| 95 | RunE: func(cmd *cobra.Command, args []string) error { |
| 96 | u, err := processParent(cmd, args) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // For CLI plugin types runtime, set extra args and settings |
| 102 | extraArgs := []string{} |
| 103 | if !ignoreFlags { |
| 104 | extraArgs = u |
| 105 | } |
| 106 | |
| 107 | // Prepare environment |
| 108 | env := os.Environ() |
| 109 | for k, v := range settings.EnvVars() { |
| 110 | env = append(env, fmt.Sprintf("%s=%s", k, v)) |
| 111 | } |
| 112 |
searching dependent graphs…