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