github.com/appscode/helm@v3.0.0-alpha.1+incompatible/cmd/helm/plugin_remove.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  	"helm.sh/helm/pkg/helmpath"
    28  	"helm.sh/helm/pkg/plugin"
    29  )
    30  
    31  type pluginRemoveOptions struct {
    32  	names []string
    33  	home  helmpath.Home
    34  }
    35  
    36  func newPluginRemoveCmd(out io.Writer) *cobra.Command {
    37  	o := &pluginRemoveOptions{}
    38  	cmd := &cobra.Command{
    39  		Use:   "remove <plugin>...",
    40  		Short: "remove one or more Helm plugins",
    41  		PreRunE: func(cmd *cobra.Command, args []string) error {
    42  			return o.complete(args)
    43  		},
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			return o.run(out)
    46  		},
    47  	}
    48  	return cmd
    49  }
    50  
    51  func (o *pluginRemoveOptions) complete(args []string) error {
    52  	if len(args) == 0 {
    53  		return errors.New("please provide plugin name to remove")
    54  	}
    55  	o.names = args
    56  	o.home = settings.Home
    57  	return nil
    58  }
    59  
    60  func (o *pluginRemoveOptions) run(out io.Writer) error {
    61  	debug("loading installed plugins from %s", settings.PluginDirs())
    62  	plugins, err := findPlugins(settings.PluginDirs())
    63  	if err != nil {
    64  		return err
    65  	}
    66  	var errorPlugins []string
    67  	for _, name := range o.names {
    68  		if found := findPlugin(plugins, name); found != nil {
    69  			if err := removePlugin(found); err != nil {
    70  				errorPlugins = append(errorPlugins, fmt.Sprintf("Failed to remove plugin %s, got error (%v)", name, err))
    71  			} else {
    72  				fmt.Fprintf(out, "Removed 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 removePlugin(p *plugin.Plugin) error {
    85  	if err := os.RemoveAll(p.Dir); err != nil {
    86  		return err
    87  	}
    88  	return runHook(p, plugin.Delete)
    89  }
    90  
    91  func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin {
    92  	for _, p := range plugins {
    93  		if p.Metadata.Name == name {
    94  			return p
    95  		}
    96  	}
    97  	return nil
    98  }