2017-04-06 14:28:09 -04:00
|
|
|
/*
|
2018-08-24 15:03:55 -04:00
|
|
|
Copyright The Helm Authors.
|
2017-04-06 14:28:09 -04:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-24 10:11:54 -05:00
|
|
|
package cmd
|
2017-04-06 14:28:09 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2025-04-10 09:06:03 -04:00
|
|
|
"log/slog"
|
2017-04-06 14:28:09 -04:00
|
|
|
|
2019-02-07 13:40:18 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
2025-02-24 10:11:54 -05:00
|
|
|
"helm.sh/helm/v4/pkg/cmd/require"
|
2024-12-26 16:33:51 -05:00
|
|
|
"helm.sh/helm/v4/pkg/plugin"
|
|
|
|
|
"helm.sh/helm/v4/pkg/plugin/installer"
|
2017-04-06 14:28:09 -04:00
|
|
|
)
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
type pluginInstallOptions struct {
|
2017-04-06 14:28:09 -04:00
|
|
|
source string
|
|
|
|
|
version string
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 10:01:15 -05:00
|
|
|
const pluginInstallDesc = `
|
|
|
|
|
This command allows you to install a plugin from a url to a VCS repo or a local path.
|
|
|
|
|
`
|
|
|
|
|
|
2017-04-06 14:28:09 -04:00
|
|
|
func newPluginInstallCmd(out io.Writer) *cobra.Command {
|
2018-05-09 13:30:32 -04:00
|
|
|
o := &pluginInstallOptions{}
|
2017-04-06 14:28:09 -04:00
|
|
|
cmd := &cobra.Command{
|
2023-12-17 22:17:53 -05:00
|
|
|
Use: "install [options] <path|url>",
|
|
|
|
|
Short: "install a Helm plugin",
|
2019-10-28 16:40:20 -04:00
|
|
|
Long: pluginInstallDesc,
|
|
|
|
|
Aliases: []string{"add"},
|
|
|
|
|
Args: require.ExactArgs(1),
|
2024-03-11 17:13:34 -04:00
|
|
|
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
|
2020-08-08 03:43:34 -04:00
|
|
|
if len(args) == 0 {
|
|
|
|
|
// We do file completion, in case the plugin is local
|
|
|
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
|
|
|
}
|
|
|
|
|
// No more completion once the plugin path has been specified
|
2024-08-17 10:00:23 -04:00
|
|
|
return noMoreArgsComp()
|
2020-08-08 03:43:34 -04:00
|
|
|
},
|
2024-03-11 17:13:34 -04:00
|
|
|
PreRunE: func(_ *cobra.Command, args []string) error {
|
2018-05-09 13:30:32 -04:00
|
|
|
return o.complete(args)
|
2017-04-06 14:28:09 -04:00
|
|
|
},
|
2024-03-11 17:13:34 -04:00
|
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
2018-05-09 13:30:32 -04:00
|
|
|
return o.run(out)
|
2017-04-06 14:28:09 -04:00
|
|
|
},
|
|
|
|
|
}
|
2018-05-09 13:30:32 -04:00
|
|
|
cmd.Flags().StringVar(&o.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed")
|
2017-04-06 14:28:09 -04:00
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
func (o *pluginInstallOptions) complete(args []string) error {
|
|
|
|
|
o.source = args[0]
|
2017-04-06 14:28:09 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
func (o *pluginInstallOptions) run(out io.Writer) error {
|
2017-04-20 02:55:36 -04:00
|
|
|
installer.Debug = settings.Debug
|
2017-04-06 14:28:09 -04:00
|
|
|
|
2019-01-14 03:11:21 -05:00
|
|
|
i, err := installer.NewForSource(o.source, o.version)
|
2017-04-06 14:28:09 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := installer.Install(i); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:06:03 -04:00
|
|
|
slog.Debug("loading plugin", "path", i.Path())
|
2017-04-06 14:28:09 -04:00
|
|
|
p, err := plugin.LoadDir(i.Path())
|
|
|
|
|
if err != nil {
|
2024-11-15 22:07:40 -05:00
|
|
|
return fmt.Errorf("plugin is installed but unusable: %w", err)
|
2017-04-06 14:28:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-08 16:08:41 -04:00
|
|
|
if err := runHook(p, plugin.Install); err != nil {
|
2017-04-06 14:28:09 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
fmt.Fprintf(out, "Installed plugin: %s\n", p.Metadata.Name)
|
2017-04-06 14:28:09 -04:00
|
|
|
return nil
|
|
|
|
|
}
|