github.com/appscode/helm@v3.0.0-alpha.1+incompatible/cmd/helm/plugin_update.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  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/spf13/cobra"
    26  
    27  	"helm.sh/helm/pkg/helmpath"
    28  	"helm.sh/helm/pkg/plugin"
    29  	"helm.sh/helm/pkg/plugin/installer"
    30  )
    31  
    32  type pluginUpdateOptions struct {
    33  	names []string
    34  	home  helmpath.Home
    35  }
    36  
    37  func newPluginUpdateCmd(out io.Writer) *cobra.Command {
    38  	o := &pluginUpdateOptions{}
    39  	cmd := &cobra.Command{
    40  		Use:   "update <plugin>...",
    41  		Short: "update one or more Helm plugins",
    42  		PreRunE: func(cmd *cobra.Command, args []string) error {
    43  			return o.complete(args)
    44  		},
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return o.run(out)
    47  		},
    48  	}
    49  	return cmd
    50  }
    51  
    52  func (o *pluginUpdateOptions) complete(args []string) error {
    53  	if len(args) == 0 {
    54  		return errors.New("please provide plugin name to update")
    55  	}
    56  	o.names = args
    57  	o.home = settings.Home
    58  	return nil
    59  }
    60  
    61  func (o *pluginUpdateOptions) run(out io.Writer) error {
    62  	installer.Debug = settings.Debug
    63  	debug("loading installed plugins from %s", settings.PluginDirs())
    64  	plugins, err := findPlugins(settings.PluginDirs())
    65  	if err != nil {
    66  		return err
    67  	}
    68  	var errorPlugins []string
    69  
    70  	for _, name := range o.names {
    71  		if found := findPlugin(plugins, name); found != nil {
    72  			if err := updatePlugin(found, o.home); err != nil {
    73  				errorPlugins = append(errorPlugins, fmt.Sprintf("Failed to update plugin %s, got error (%v)", name, err))
    74  			} else {
    75  				fmt.Fprintf(out, "Updated plugin: %s\n", name)
    76  			}
    77  		} else {
    78  			errorPlugins = append(errorPlugins, fmt.Sprintf("Plugin: %s not found", name))
    79  		}
    80  	}
    81  	if len(errorPlugins) > 0 {
    82  		return errors.Errorf(strings.Join(errorPlugins, "\n"))
    83  	}
    84  	return nil
    85  }
    86  
    87  func updatePlugin(p *plugin.Plugin, home helmpath.Home) error {
    88  	exactLocation, err := filepath.EvalSymlinks(p.Dir)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	absExactLocation, err := filepath.Abs(exactLocation)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	i, err := installer.FindSource(absExactLocation, home)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	if err := installer.Update(i); err != nil {
   102  		return err
   103  	}
   104  
   105  	debug("loading plugin from %s", i.Path())
   106  	updatedPlugin, err := plugin.LoadDir(i.Path())
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	return runHook(updatedPlugin, plugin.Update)
   112  }