-
Notifications
You must be signed in to change notification settings - Fork 20
/
sshkey_delete.go
74 lines (63 loc) · 1.52 KB
/
sshkey_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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var sshkeyDeleteCmd = &cobra.Command{
Use: "delete [name]+",
Short: "Delete SSH key pair",
Aliases: gDeleteAlias,
RunE: func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
if len(args) < 1 && !all {
return cmd.Usage()
}
sshKeys := []egoscale.SSHKeyPair{}
if all {
sshKeys, err = getSSHKeys(cs)
if err != nil {
return err
}
} else {
for _, k := range args {
sshKeys = append(sshKeys, egoscale.SSHKeyPair{Name: k})
}
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
tasks := make([]task, 0, len(sshKeys))
for _, sshkey := range sshKeys {
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete %q SSH key pair", sshkey.Name)) {
continue
}
}
cmd := &egoscale.DeleteSSHKeyPair{Name: sshkey.Name}
tasks = append(tasks, task{
cmd,
fmt.Sprintf("delete %q key pair", cmd.Name),
})
}
resps := asyncTasks(tasks)
errs := filterErrors(resps)
if len(errs) > 0 {
return errs[0]
}
return nil
},
}
func deleteSSHKey(name string) error {
sshKey := &egoscale.DeleteSSHKeyPair{Name: name}
return cs.BooleanRequestWithContext(gContext, sshKey)
}
func init() {
sshkeyDeleteCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
sshkeyDeleteCmd.Flags().BoolP("all", "", false, "Remove all SSH keys")
sshkeyCmd.AddCommand(sshkeyDeleteCmd)
}