-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_delete.go
82 lines (68 loc) · 1.64 KB
/
vm_delete.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
package cmd
import (
"fmt"
"os"
"path"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmDeleteCmd = &cobra.Command{
Use: "delete NAME|ID",
Short: "Delete a Compute instance",
Aliases: gDeleteAlias,
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
tasks := []task{}
vms := make([]egoscale.VirtualMachine, len(args))
for i, arg := range args {
vm, err := getVirtualMachineByNameOrID(arg)
if err != nil {
return err
}
vms[i] = *vm
if ok := prepareDeleteVM(vm, force); ok {
tasks = append(tasks, task{
&egoscale.DestroyVirtualMachine{ID: vm.ID},
fmt.Sprintf("Destroying Compute instance %q", vm.Name),
})
}
}
resps := asyncTasks(tasks)
errors := filterErrors(resps)
if len(errors) > 0 {
for _, err := range errors {
fmt.Fprintln(os.Stderr, err) // nolint: errcheck
}
return nil
}
for i := range resps {
vm := vms[i]
folder := path.Join(gConfigFolder, "instances", vm.ID.String())
if _, err := os.Stat(folder); !os.IsNotExist(err) {
if err := os.RemoveAll(folder); err != nil {
return err
}
}
}
return nil
},
}
func prepareDeleteVM(vm *egoscale.VirtualMachine, force bool) bool {
if !force {
if !askQuestion(fmt.Sprintf("Are you you want to delete Compute instance %q?", vm.Name)) {
return false
}
}
return true
}
func init() {
vmDeleteCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
vmCmd.AddCommand(vmDeleteCmd)
}