github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/plugin/uninstall.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package plugin
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  
    26  	"github.com/pkg/errors"
    27  	"github.com/spf13/cobra"
    28  	"k8s.io/cli-runtime/pkg/genericiooptions"
    29  	"k8s.io/klog/v2"
    30  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    31  	"k8s.io/kubectl/pkg/util/templates"
    32  
    33  	"github.com/1aal/kubeblocks/pkg/cli/util"
    34  )
    35  
    36  var (
    37  	pluginUninstallExample = templates.Examples(`
    38  	# uninstall a kbcli or kubectl plugin by name
    39  	kbcli plugin uninstall [PLUGIN]
    40  	`)
    41  )
    42  
    43  func NewPluginUninstallCmd(_ genericiooptions.IOStreams) *cobra.Command {
    44  	cmd := &cobra.Command{
    45  		Use:     "uninstall",
    46  		Short:   "Uninstall kbcli or kubectl plugins",
    47  		Example: pluginUninstallExample,
    48  		Run: func(cmd *cobra.Command, args []string) {
    49  			cmdutil.CheckErr(uninstallPlugins(args))
    50  		},
    51  	}
    52  
    53  	return cmd
    54  }
    55  
    56  func uninstallPlugins(names []string) error {
    57  	for _, name := range names {
    58  		klog.V(4).Infof("Going to uninstall plugin %s\n", name)
    59  		if err := uninstall(paths, name); err != nil {
    60  			return errors.Wrapf(err, "failed to uninstall plugin %s", name)
    61  		}
    62  	}
    63  	return nil
    64  }
    65  
    66  func uninstall(p *Paths, name string) error {
    67  	if _, err := ReadReceiptFromFile(p.PluginInstallReceiptPath(name)); err != nil {
    68  		if os.IsNotExist(err) {
    69  			return ErrIsNotInstalled
    70  		}
    71  		return errors.Wrapf(err, "failed to look up install receipt for plugin %q", name)
    72  	}
    73  
    74  	klog.V(1).Infof("Deleting plugin %s", name)
    75  
    76  	symlinkPath := filepath.Join(p.BinPath(), pluginNameToBin(name, util.IsWindows()))
    77  	klog.V(3).Infof("Unlink %q", symlinkPath)
    78  	if err := removeLink(symlinkPath); err != nil {
    79  		return errors.Wrap(err, "could not uninstall symlink of plugin")
    80  	}
    81  
    82  	pluginInstallPath := p.PluginInstallPath(name)
    83  	klog.V(3).Infof("Deleting path %q", pluginInstallPath)
    84  	if err := os.RemoveAll(pluginInstallPath); err != nil {
    85  		return errors.Wrapf(err, "could not remove plugin directory %q", pluginInstallPath)
    86  	}
    87  	pluginReceiptPath := p.PluginInstallReceiptPath(name)
    88  	klog.V(3).Infof("Deleting plugin receipt %q", pluginReceiptPath)
    89  	err := os.Remove(pluginReceiptPath)
    90  	return errors.Wrapf(err, "could not remove plugin receipt %q", pluginReceiptPath)
    91  }