/
remove.go
81 lines (66 loc) · 1.84 KB
/
remove.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
package cli
import (
"fmt"
"strings"
"github.com/99designs/aws-vault/v6/prompt"
"github.com/99designs/aws-vault/v6/vault"
"github.com/99designs/keyring"
"github.com/alecthomas/kingpin"
)
type RemoveCommandInput struct {
ProfileName string
SessionsOnly bool
Force bool
}
func ConfigureRemoveCommand(app *kingpin.Application, a *AwsVault) {
input := RemoveCommandInput{}
cmd := app.Command("remove", "Removes credentials from the secure keystore")
cmd.Alias("rm")
cmd.Arg("profile", "Name of the profile").
Required().
HintAction(a.MustGetProfileNames).
StringVar(&input.ProfileName)
cmd.Flag("sessions-only", "Only remove sessions, leave credentials intact").
Short('s').
Hidden().
BoolVar(&input.SessionsOnly)
cmd.Flag("force", "Force-remove the profile without a prompt").
Short('f').
BoolVar(&input.Force)
cmd.Action(func(c *kingpin.ParseContext) error {
keyring, err := a.Keyring()
if err != nil {
return err
}
err = RemoveCommand(input, keyring)
app.FatalIfError(err, "remove")
return nil
})
}
func RemoveCommand(input RemoveCommandInput, keyring keyring.Keyring) error {
ckr := &vault.CredentialKeyring{Keyring: keyring}
// Legacy --sessions-only option for backwards compatibility, use aws-vault clear instead
if input.SessionsOnly {
sk := &vault.SessionKeyring{Keyring: ckr.Keyring}
n, err := sk.RemoveForProfile(input.ProfileName)
if err != nil {
return err
}
fmt.Printf("Deleted %d sessions.\n", n)
return nil
}
if !input.Force {
r, err := prompt.TerminalPrompt(fmt.Sprintf("Delete credentials for profile %q? (y|N) ", input.ProfileName))
if err != nil {
return err
}
if !strings.EqualFold(r, "y") && !strings.EqualFold(r, "yes") {
return nil
}
}
if err := ckr.Remove(input.ProfileName); err != nil {
return err
}
fmt.Printf("Deleted credentials.\n")
return nil
}