github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/plugin_uninstall.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 "os" 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 ) 30 31 type pluginUninstallOptions struct { 32 names []string 33 } 34 35 func newPluginUninstallCmd(out io.Writer) *cobra.Command { 36 o := &pluginUninstallOptions{} 37 38 cmd := &cobra.Command{ 39 Use: "uninstall <plugin>...", 40 Aliases: []string{"rm", "remove"}, 41 Short: "uninstall 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 50 // Function providing dynamic auto-completion 51 completion.RegisterValidArgsFunc(cmd, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { 52 if len(args) != 0 { 53 return nil, completion.BashCompDirectiveNoFileComp 54 } 55 return compListPlugins(toComplete), completion.BashCompDirectiveNoFileComp 56 }) 57 58 return cmd 59 } 60 61 func (o *pluginUninstallOptions) complete(args []string) error { 62 if len(args) == 0 { 63 return errors.New("please provide plugin name to uninstall") 64 } 65 o.names = args 66 return nil 67 } 68 69 func (o *pluginUninstallOptions) run(out io.Writer) error { 70 debug("loading installed plugins from %s", settings.PluginsDirectory) 71 plugins, err := findPlugins(settings.PluginsDirectory) 72 if err != nil { 73 return err 74 } 75 var errorPlugins []string 76 for _, name := range o.names { 77 if found := findPlugin(plugins, name); found != nil { 78 if err := uninstallPlugin(found); err != nil { 79 errorPlugins = append(errorPlugins, fmt.Sprintf("Failed to uninstall plugin %s, got error (%v)", name, err)) 80 } else { 81 fmt.Fprintf(out, "Uninstalled plugin: %s\n", name) 82 } 83 } else { 84 errorPlugins = append(errorPlugins, fmt.Sprintf("Plugin: %s not found", name)) 85 } 86 } 87 if len(errorPlugins) > 0 { 88 return errors.Errorf(strings.Join(errorPlugins, "\n")) 89 } 90 return nil 91 } 92 93 func uninstallPlugin(p *plugin.Plugin) error { 94 if err := os.RemoveAll(p.Dir); err != nil { 95 return err 96 } 97 return runHook(p, plugin.Delete) 98 } 99 100 func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin { 101 for _, p := range plugins { 102 if p.Metadata.Name == name { 103 return p 104 } 105 } 106 return nil 107 }