github.com/diafour/helm@v3.0.0-beta.3+incompatible/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/spf13/cobra"
    23  
    24  	"helm.sh/helm/cmd/helm/require"
    25  	"helm.sh/helm/pkg/plugin"
    26  	"helm.sh/helm/pkg/plugin/installer"
    27  )
    28  
    29  type pluginInstallOptions struct {
    30  	source  string
    31  	version string
    32  }
    33  
    34  const pluginInstallDesc = `
    35  This command allows you to install a plugin from a url to a VCS repo or a local path.
    36  
    37  Example usage:
    38      $ helm plugin install https://github.com/technosophos/helm-template
    39  `
    40  
    41  func newPluginInstallCmd(out io.Writer) *cobra.Command {
    42  	o := &pluginInstallOptions{}
    43  	cmd := &cobra.Command{
    44  		Use:   "install [options] <path|url>...",
    45  		Short: "install one or more Helm plugins",
    46  		Long:  pluginInstallDesc,
    47  		Args:  require.ExactArgs(1),
    48  		PreRunE: func(cmd *cobra.Command, args []string) error {
    49  			return o.complete(args)
    50  		},
    51  		RunE: func(cmd *cobra.Command, args []string) error {
    52  			return o.run(out)
    53  		},
    54  	}
    55  	cmd.Flags().StringVar(&o.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed")
    56  	return cmd
    57  }
    58  
    59  func (o *pluginInstallOptions) complete(args []string) error {
    60  	o.source = args[0]
    61  	return nil
    62  }
    63  
    64  func (o *pluginInstallOptions) run(out io.Writer) error {
    65  	installer.Debug = settings.Debug
    66  
    67  	i, err := installer.NewForSource(o.source, o.version)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	if err := installer.Install(i); err != nil {
    72  		return err
    73  	}
    74  
    75  	debug("loading plugin from %s", i.Path())
    76  	p, err := plugin.LoadDir(i.Path())
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	if err := runHook(p, plugin.Install); err != nil {
    82  		return err
    83  	}
    84  
    85  	fmt.Fprintf(out, "Installed plugin: %s\n", p.Metadata.Name)
    86  	return nil
    87  }