github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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  	"fmt"
    20  	"io"
    21  	"os"
    22  
    23  	"k8s.io/helm/pkg/helm/helmpath"
    24  	"k8s.io/helm/pkg/plugin"
    25  
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  type pluginRemoveCmd struct {
    30  	names []string
    31  	home  helmpath.Home
    32  	out   io.Writer
    33  }
    34  
    35  func newPluginRemoveCmd(out io.Writer) *cobra.Command {
    36  	pcmd := &pluginRemoveCmd{out: out}
    37  	cmd := &cobra.Command{
    38  		Use:   "remove <plugin>...",
    39  		Short: "remove one or more Helm plugins",
    40  		PreRunE: func(cmd *cobra.Command, args []string) error {
    41  			return pcmd.complete(args)
    42  		},
    43  		RunE: func(cmd *cobra.Command, args []string) error {
    44  			return pcmd.run()
    45  		},
    46  	}
    47  	return cmd
    48  }
    49  
    50  func (pcmd *pluginRemoveCmd) complete(args []string) error {
    51  	if err := checkArgsLength(len(args), "plugin"); err != nil {
    52  		return err
    53  	}
    54  	pcmd.names = args
    55  	pcmd.home = helmpath.Home(homePath())
    56  	return nil
    57  }
    58  
    59  func (pcmd *pluginRemoveCmd) run() error {
    60  	plugdirs := pluginDirs(pcmd.home)
    61  	debug("loading installed plugins from %s", plugdirs)
    62  	plugins, err := findPlugins(plugdirs)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	for _, name := range pcmd.names {
    68  		if found := findPlugin(plugins, name); found != nil {
    69  			if err := removePlugin(found, pcmd.home); err != nil {
    70  				return err
    71  			}
    72  			fmt.Fprintf(pcmd.out, "Removed plugin: %s\n", name)
    73  		}
    74  	}
    75  	return nil
    76  }
    77  
    78  func removePlugin(p *plugin.Plugin, home helmpath.Home) error {
    79  	if err := os.Remove(p.Dir); err != nil {
    80  		return err
    81  	}
    82  	return runHook(p, plugin.Delete, home)
    83  }
    84  
    85  func findPlugin(plugins []*plugin.Plugin, name string) *plugin.Plugin {
    86  	for _, p := range plugins {
    87  		if p.Metadata.Name == name {
    88  			return p
    89  		}
    90  	}
    91  	return nil
    92  }