Skip to content

Commit 5c89378

Browse files
committed
Warning if removes a non-exists plugin
1 parent c2f3a9c commit 5c89378

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

cmd/plugin.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"sort"
78
"strings"
89

@@ -253,13 +254,18 @@ var pluginUninstallCmd = &cobra.Command{
253254
)
254255

255256
for _, name := range names {
257+
entry, exists := manifest.Get(name)
258+
if !exists && !managedPluginBinaryExists(name) {
259+
terminal.Warningf("Plugin %q is not installed; skipping.", name)
260+
continue
261+
}
262+
256263
spinner := uicli.NewSpinner().
257264
WithStyle(uicli.SpinnerDots).
258265
WithColor(uicli.CyanColor).
259266
WithMessage(fmt.Sprintf("Removing plugin %q...", name)).
260267
Start()
261268

262-
entry, _ := manifest.Get(name)
263269
inst, err := installer.FromManifest(entry)
264270
if err != nil {
265271
// If we can't determine the installer type, just remove the binary directly.
@@ -490,6 +496,20 @@ func uniquePluginNames(args []string) []string {
490496
return names
491497
}
492498

499+
func managedPluginBinaryExists(name string) bool {
500+
dir, err := plugin.PluginBinDir()
501+
if err != nil {
502+
return false
503+
}
504+
505+
path := filepath.Join(dir, plugin.BinPrefix+name)
506+
info, err := os.Stat(path)
507+
if err != nil {
508+
return false
509+
}
510+
return !info.IsDir()
511+
}
512+
493513
func ensureInstallNameAvailable(manifest *plugin.Manifest, name string) error {
494514
if _, exists := manifest.Get(name); exists {
495515
return fmt.Errorf("plugin %q already exists; use \"clime plugin update %s\" or uninstall it first", name, name)

cmd/plugin_uninstall_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cmd
22

3-
import "testing"
3+
import (
4+
"strings"
5+
"testing"
6+
)
47

58
func TestUniquePluginNames(t *testing.T) {
69
t.Parallel()
@@ -37,3 +40,20 @@ func TestPluginUninstallArgsAllowsMultiple(t *testing.T) {
3740
t.Fatal("zero args should fail")
3841
}
3942
}
43+
44+
func TestPluginUninstallWarnsWhenPluginDoesNotExist(t *testing.T) {
45+
t.Setenv("HOME", t.TempDir())
46+
47+
output := captureStdout(t, func() {
48+
if err := pluginUninstallCmd.RunE(pluginUninstallCmd, []string{"missing-plugin"}); err != nil {
49+
t.Fatalf("pluginUninstallCmd.RunE() error = %v", err)
50+
}
51+
})
52+
53+
if !strings.Contains(output, `Plugin "missing-plugin" is not installed; skipping.`) {
54+
t.Fatalf("stdout = %q, want missing-plugin warning", output)
55+
}
56+
if strings.Contains(output, `Removed plugin "missing-plugin"`) {
57+
t.Fatalf("stdout = %q, should not report plugin removal", output)
58+
}
59+
}

0 commit comments

Comments
 (0)