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