-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
95 lines (80 loc) · 2.02 KB
/
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
83
84
85
86
87
88
89
90
91
92
93
94
95
package keys
import (
"bufio"
"errors"
"github.com/spf13/viper"
"github.com/hdac-io/friday/client/input"
"github.com/hdac-io/friday/crypto/keys"
"github.com/spf13/cobra"
)
const (
flagYes = "yes"
flagForce = "force"
)
func deleteKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <name>",
Short: "Delete the given key",
Long: `Delete a key from the store.
Note that removing offline or ledger keys will remove
only the public key references stored locally, i.e.
private keys stored in a ledger device cannot be deleted with the CLI.
`,
RunE: runDeleteCmd,
Args: cobra.ExactArgs(1),
}
cmd.Flags().BoolP(flagYes, "y", false,
"Skip confirmation prompt when deleting offline or ledger key references")
cmd.Flags().BoolP(flagForce, "f", false,
"Remove the key unconditionally without asking for the passphrase")
return cmd
}
func runDeleteCmd(cmd *cobra.Command, args []string) error {
name := args[0]
kb, err := NewKeyBaseFromHomeFlag()
if err != nil {
return err
}
info, err := kb.Get(name)
if err != nil {
return err
}
buf := bufio.NewReader(cmd.InOrStdin())
if info.GetType() == keys.TypeLedger || info.GetType() == keys.TypeOffline {
if !viper.GetBool(flagYes) {
if err := confirmDeletion(buf); err != nil {
return err
}
}
if err := kb.Delete(name, "", true); err != nil {
return err
}
cmd.PrintErrln("Public key reference deleted")
return nil
}
// skip passphrase check if run with --force
skipPass := viper.GetBool(flagForce)
var oldpass string
if !skipPass {
if oldpass, err = input.GetPassword(
"DANGER - enter password to permanently delete key:", buf); err != nil {
return err
}
}
err = kb.Delete(name, oldpass, skipPass)
if err != nil {
return err
}
cmd.PrintErrln("Key deleted forever (uh oh!)")
return nil
}
func confirmDeletion(buf *bufio.Reader) error {
answer, err := input.GetConfirmation("Key reference will be deleted. Continue?", buf)
if err != nil {
return err
}
if !answer {
return errors.New("aborted")
}
return nil
}