github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/cmd/helm/plugin_install.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"k8s.io/helm/pkg/helm/helmpath"
    23  	"k8s.io/helm/pkg/plugin"
    24  	"k8s.io/helm/pkg/plugin/installer"
    25  
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  type pluginInstallCmd struct {
    30  	source  string
    31  	version string
    32  	home    helmpath.Home
    33  	out     io.Writer
    34  }
    35  
    36  func newPluginInstallCmd(out io.Writer) *cobra.Command {
    37  	pcmd := &pluginInstallCmd{out: out}
    38  	cmd := &cobra.Command{
    39  		Use:   "install [options] <path|url>...",
    40  		Short: "install one or more Helm plugins",
    41  		PreRunE: func(cmd *cobra.Command, args []string) error {
    42  			return pcmd.complete(args)
    43  		},
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			return pcmd.run()
    46  		},
    47  	}
    48  	cmd.Flags().StringVar(&pcmd.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed")
    49  	return cmd
    50  }
    51  
    52  func (pcmd *pluginInstallCmd) complete(args []string) error {
    53  	if err := checkArgsLength(len(args), "plugin"); err != nil {
    54  		return err
    55  	}
    56  	pcmd.source = args[0]
    57  	pcmd.home = helmpath.Home(homePath())
    58  	return nil
    59  }
    60  
    61  func (pcmd *pluginInstallCmd) run() error {
    62  	installer.Debug = flagDebug
    63  
    64  	i, err := installer.NewForSource(pcmd.source, pcmd.version, pcmd.home)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	if err := installer.Install(i); err != nil {
    69  		return err
    70  	}
    71  
    72  	debug("loading plugin from %s", i.Path())
    73  	p, err := plugin.LoadDir(i.Path())
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	if err := runHook(p, plugin.Install, pcmd.home); err != nil {
    79  		return err
    80  	}
    81  
    82  	fmt.Fprintf(pcmd.out, "Installed plugin: %s\n", p.Metadata.Name)
    83  	return nil
    84  }