github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/cmd/helm/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/stefanmcshane/helm/pkg/plugin" 28 ) 29 30 type pluginUninstallOptions struct { 31 names []string 32 } 33 34 func newPluginUninstallCmd(out io.Writer) *cobra.Command { 35 o := &pluginUninstallOptions{} 36 37 cmd := &cobra.Command{ 38 Use: "uninstall <plugin>...", 39 Aliases: []string{"rm", "remove"}, 40 Short: "uninstall one or more Helm plugins", 41 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 42 return compListPlugins(toComplete, args), cobra.ShellCompDirectiveNoFileComp 43 }, 44 PreRunE: func(cmd *cobra.Command, args []string) error { 45 return o.complete(args) 46 }, 47 RunE: func(cmd *cobra.Command, args []string) error { 48 return o.run(out) 49 }, 50 } 51 return cmd 52 } 53 54 func (o *pluginUninstallOptions) complete(args []string) error { 55 if len(args) == 0 { 56 return errors.New("please provide plugin name to uninstall") 57 } 58 o.names = args 59 return nil 60 } 61 62 func (o *pluginUninstallOptions) run(out io.Writer) error { 63 debug("loading installed plugins from %s", settings.PluginsDirectory) 64 plugins, err := plugin.FindPlugins(settings.PluginsDirectory) 65 if err != nil { 66 return err 67 } 68 var errorPlugins []string 69 for _, name := range o.names { 70 if found := findPlugin(plugins, name); found != nil { 71 if err := uninstallPlugin(found); err != nil { 72 errorPlugins = append(errorPlugins, fmt.Sprintf("Failed to uninstall plugin %s, got error (%v)", name, err)) 73 } else { 74 fmt.Fprintf(out, "Uninstalled plugin: %s\n", name) 75 } 76 } else { 77 errorPlugins = append(errorPlugins, fmt.Sprintf("Plugin: %s not found", name)) 78 } 79 } 80 if len(errorPlugins) > 0 { 81 return errors.Errorf(strings.Join(errorPlugins, "\n")) 82 } 83 return nil 84 } 85 86 func uninstallPlugin(p *plugin.Plugin) error { 87 if err := os.RemoveAll(p.Dir); err != nil { 88 return err 89 } 90 return runHook(p, plugin.Delete) 91 } 92 93 func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin { 94 for _, p := range plugins { 95 if p.Metadata.Name == name { 96 return p 97 } 98 } 99 return nil 100 }