forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.go
91 lines (75 loc) · 1.91 KB
/
uninstall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package pluginaction
import (
"fmt"
"os"
"os/exec"
"time"
)
// PluginNotFoundError is an error returned when a plugin is not found.
type PluginNotFoundError struct {
PluginName string
}
// Error outputs a plugin not found error message.
func (e PluginNotFoundError) Error() string {
return fmt.Sprintf("Plugin name %s does not exist.", e.PluginName)
}
//go:generate counterfeiter . PluginUninstaller
type PluginUninstaller interface {
Run(pluginPath string, command string) error
}
// PluginBinaryRemoveFailedError is returned when running the plugin binary fails.
type PluginBinaryRemoveFailedError struct {
Err error
}
func (p PluginBinaryRemoveFailedError) Error() string {
return p.Err.Error()
}
// PluginExecuteError is returned when running the plugin binary fails.
type PluginExecuteError struct {
Err error
}
func (p PluginExecuteError) Error() string {
return p.Err.Error()
}
func (actor Actor) UninstallPlugin(uninstaller PluginUninstaller, name string) error {
plugin, exist := actor.config.GetPlugin(name)
if !exist {
return PluginNotFoundError{PluginName: name}
}
var binaryErr error
if actor.FileExists(plugin.Location) {
err := uninstaller.Run(plugin.Location, "CLI-MESSAGE-UNINSTALL")
if err != nil {
switch err.(type) {
case *exec.ExitError:
binaryErr = PluginExecuteError{
Err: err,
}
case *os.PathError:
binaryErr = PluginExecuteError{
Err: err,
}
default:
return err
}
}
// No test for sleeping for 500 ms for parity with pre-refactored behavior.
time.Sleep(500 * time.Millisecond)
err = os.Remove(plugin.Location)
if err != nil && !os.IsNotExist(err) {
if _, isPathError := err.(*os.PathError); isPathError {
binaryErr = PluginBinaryRemoveFailedError{
Err: err,
}
} else {
return err
}
}
}
actor.config.RemovePlugin(name)
err := actor.config.WritePluginConfig()
if err != nil {
return err
}
return binaryErr
}