github.com/vtuson/helm@v2.8.2+incompatible/cmd/helm/plugin_remove.go (about)

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