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