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