forked from 99designs/aws-vault
/
rm.go
69 lines (57 loc) · 1.55 KB
/
rm.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
package cli
import (
"fmt"
"github.com/99designs/aws-vault/prompt"
"github.com/99designs/aws-vault/vault"
"github.com/99designs/keyring"
"gopkg.in/alecthomas/kingpin.v2"
)
type RemoveCommandInput struct {
Profile string
Keyring keyring.Keyring
SessionsOnly bool
}
func ConfigureRemoveCommand(app *kingpin.Application) {
input := RemoveCommandInput{}
cmd := app.Command("remove", "Removes credentials, including sessions")
cmd.Alias("rm")
cmd.Arg("profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Flag("sessions-only", "Only remove sessions, leave credentials intact").
Short('s').
BoolVar(&input.SessionsOnly)
cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
RemoveCommand(app, input)
return nil
})
}
func RemoveCommand(app *kingpin.Application, input RemoveCommandInput) {
if !input.SessionsOnly {
provider := &vault.KeyringProvider{Keyring: input.Keyring, Profile: input.Profile}
r, err := prompt.TerminalPrompt(fmt.Sprintf("Delete credentials for profile %q? (Y|n)", input.Profile))
if err != nil {
app.Fatalf(err.Error())
return
} else if r == "N" || r == "n" {
return
}
if err := provider.Delete(); err != nil {
app.Fatalf(err.Error())
return
}
fmt.Printf("Deleted credentials.\n")
}
sessions, err := vault.NewKeyringSessions(input.Keyring, awsConfig)
if err != nil {
app.Fatalf(err.Error())
return
}
n, err := sessions.Delete(input.Profile)
if err != nil {
app.Fatalf(err.Error())
return
}
fmt.Printf("Deleted %d sessions.\n", n)
}