github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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/v3/cmd/helm/require"
    25  	"helm.sh/helm/v3/pkg/plugin"
    26  	"helm.sh/helm/v3/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  
    38  func newPluginInstallCmd(out io.Writer) *cobra.Command {
    39  	o := &pluginInstallOptions{}
    40  	cmd := &cobra.Command{
    41  		Use:     "install [options] <path|url>...",
    42  		Short:   "install one or more Helm plugins",
    43  		Long:    pluginInstallDesc,
    44  		Aliases: []string{"add"},
    45  		Args:    require.ExactArgs(1),
    46  		PreRunE: func(cmd *cobra.Command, args []string) error {
    47  			return o.complete(args)
    48  		},
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			return o.run(out)
    51  		},
    52  	}
    53  	cmd.Flags().StringVar(&o.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed")
    54  	return cmd
    55  }
    56  
    57  func (o *pluginInstallOptions) complete(args []string) error {
    58  	o.source = args[0]
    59  	return nil
    60  }
    61  
    62  func (o *pluginInstallOptions) run(out io.Writer) error {
    63  	installer.Debug = settings.Debug
    64  
    65  	i, err := installer.NewForSource(o.source, o.version)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if err := installer.Install(i); err != nil {
    70  		return err
    71  	}
    72  
    73  	debug("loading plugin from %s", i.Path())
    74  	p, err := plugin.LoadDir(i.Path())
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	if err := runHook(p, plugin.Install); err != nil {
    80  		return err
    81  	}
    82  
    83  	fmt.Fprintf(out, "Installed plugin: %s\n", p.Metadata.Name)
    84  	return nil
    85  }