github.com/loafoe/helm@v1.0.1/cmd/helm/plugin_install.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package main 17 18 import ( 19 "fmt" 20 "io" 21 22 "github.com/pkg/errors" 23 "github.com/spf13/cobra" 24 25 "helm.sh/helm/v3/cmd/helm/require" 26 "helm.sh/helm/v3/pkg/plugin" 27 "helm.sh/helm/v3/pkg/plugin/installer" 28 ) 29 30 type pluginInstallOptions struct { 31 source string 32 version string 33 } 34 35 const pluginInstallDesc = ` 36 This command allows you to install a plugin from a url to a VCS repo or a local path. 37 ` 38 39 func newPluginInstallCmd(out io.Writer) *cobra.Command { 40 o := &pluginInstallOptions{} 41 cmd := &cobra.Command{ 42 Use: "install [options] <path|url>...", 43 Short: "install one or more Helm plugins", 44 Long: pluginInstallDesc, 45 Aliases: []string{"add"}, 46 Args: require.ExactArgs(1), 47 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 48 if len(args) == 0 { 49 // We do file completion, in case the plugin is local 50 return nil, cobra.ShellCompDirectiveDefault 51 } 52 // No more completion once the plugin path has been specified 53 return nil, cobra.ShellCompDirectiveNoFileComp 54 }, 55 PreRunE: func(cmd *cobra.Command, args []string) error { 56 return o.complete(args) 57 }, 58 RunE: func(cmd *cobra.Command, args []string) error { 59 return o.run(out) 60 }, 61 } 62 cmd.Flags().StringVar(&o.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed") 63 return cmd 64 } 65 66 func (o *pluginInstallOptions) complete(args []string) error { 67 o.source = args[0] 68 return nil 69 } 70 71 func (o *pluginInstallOptions) run(out io.Writer) error { 72 installer.Debug = settings.Debug 73 74 i, err := installer.NewForSource(o.source, o.version) 75 if err != nil { 76 return err 77 } 78 if err := installer.Install(i); err != nil { 79 return err 80 } 81 82 debug("loading plugin from %s", i.Path()) 83 p, err := plugin.LoadDir(i.Path()) 84 if err != nil { 85 return errors.Wrap(err, "plugin is installed but unusable") 86 } 87 88 if err := runHook(p, plugin.Install); err != nil { 89 return err 90 } 91 92 fmt.Fprintf(out, "Installed plugin: %s\n", p.Metadata.Name) 93 return nil 94 }