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