forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.go
89 lines (70 loc) · 1.89 KB
/
rm.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
package commands
import (
"fmt"
"strings"
"errors"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/log"
)
func cmdRm(c CommandLine, api libmachine.API) error {
if len(c.Args()) == 0 {
c.ShowHelp()
return ErrNoMachineSpecified
}
log.Info(fmt.Sprintf("About to remove %s", strings.Join(c.Args(), ", ")))
log.Warn("WARNING: This action will delete both local reference and remote instance.")
force := c.Bool("force")
confirm := c.Bool("y")
var errorOccurred []string
if !userConfirm(confirm, force) {
return nil
}
for _, hostName := range c.Args() {
err := removeRemoteMachine(hostName, api)
if err != nil {
errorOccurred = collectError(fmt.Sprintf("Error removing host %q: %s", hostName, err), force, errorOccurred)
}
if err == nil || force {
removeErr := removeLocalMachine(hostName, api)
if removeErr != nil {
errorOccurred = collectError(fmt.Sprintf("Can't remove \"%s\"", hostName), force, errorOccurred)
} else {
log.Infof("Successfully removed %s", hostName)
}
}
}
if len(errorOccurred) > 0 && !force {
return errors.New(strings.Join(errorOccurred, "\n"))
}
return nil
}
func userConfirm(confirm bool, force bool) bool {
if confirm || force {
return true
}
sure, err := confirmInput(fmt.Sprintf("Are you sure?"))
if err != nil {
return false
}
return sure
}
func removeRemoteMachine(hostName string, api libmachine.API) error {
currentHost, loaderr := api.Load(hostName)
if loaderr != nil {
return loaderr
}
return currentHost.Driver.Remove()
}
func removeLocalMachine(hostName string, api libmachine.API) error {
exist, _ := api.Exists(hostName)
if !exist {
return errors.New(hostName + " does not exist.")
}
return api.Remove(hostName)
}
func collectError(message string, force bool, errorOccurred []string) []string {
if force {
log.Error(message)
}
return append(errorOccurred, message)
}