forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.go
81 lines (67 loc) · 1.54 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
package pluginaction
import (
"os"
"os/exec"
"time"
"code.cloudfoundry.org/cli/actor/actionerror"
)
//go:generate counterfeiter . PluginUninstaller
type PluginUninstaller interface {
Run(pluginPath string, command string) error
}
func (actor Actor) UninstallPlugin(uninstaller PluginUninstaller, name string) error {
plugin, exist := actor.config.GetPlugin(name)
if !exist {
return actionerror.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 = actionerror.PluginExecuteError{
Err: err,
}
case *os.PathError:
binaryErr = actionerror.PluginExecuteError{
Err: err,
}
default:
return err
}
}
// No test for sleeping for 500 ms for parity with pre-refactored behavior.
time.Sleep(500 * time.Millisecond)
removed := false
for i := 0; i < 50; i++ {
err = os.Remove(plugin.Location)
if err == nil {
removed = true
break
}
if os.IsNotExist(err) {
removed = true
break
}
if err != nil && !os.IsNotExist(err) {
if _, isPathError := err.(*os.PathError); isPathError {
time.Sleep(50 * time.Millisecond)
} else {
return err
}
}
}
if !removed {
binaryErr = actionerror.PluginBinaryRemoveFailedError{
Err: err,
}
}
}
actor.config.RemovePlugin(name)
err := actor.config.WritePluginConfig()
if err != nil {
return err
}
return binaryErr
}