MCPcopy
hub / github.com/helm/helm / loadCLIPlugins

Function loadCLIPlugins

pkg/cmd/load_plugins.go:55–157  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

53// to inspect its environment and then add commands to the base command
54// as it finds them.
55func 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

Callers 5

TestLoadCLIPluginsFunction · 0.85
TestLoadPluginsWithSpaceFunction · 0.85
newRootCmdWithConfigFunction · 0.85

Calls 11

FindPluginsFunction · 0.92
processParentFunction · 0.85
loadCompletionForPluginFunction · 0.85
EnvVarsMethod · 0.80
MetadataMethod · 0.65
InvokeMethod · 0.65
NameMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45
ParentMethod · 0.45
RootMethod · 0.45

Tested by 4

TestLoadCLIPluginsFunction · 0.68
TestLoadPluginsWithSpaceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…