-
Notifications
You must be signed in to change notification settings - Fork 20
/
sshkey_delete.go
70 lines (58 loc) · 1.49 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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
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
}
//XXX Create a function to execute non async tasks asynchronousely
for _, sshkey := range sshKeys {
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete %q SSH key pair", sshkey.Name)) {
continue
}
}
if err := deleteSSHKey(sshkey.Name); err != nil {
return err
}
fmt.Println(sshkey.Name)
}
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, "Attempt to remove SSH key pair without prompting for confirmation")
sshkeyDeleteCmd.Flags().BoolP("all", "", false, "Remove all SSH keys")
sshkeyCmd.AddCommand(sshkeyDeleteCmd)
}