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