-
Notifications
You must be signed in to change notification settings - Fork 41
/
whoami.go
59 lines (51 loc) · 1.27 KB
/
whoami.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
package cmd
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/netflix/weep/pkg/aws"
"github.com/netflix/weep/pkg/swag"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
rootCmd.AddCommand(whoamiCmd)
}
var whoamiCmd = &cobra.Command{
Use: "whoami",
Short: whoamiShortHelp,
Long: whoamiLongHelp,
RunE: runWhoami,
SilenceUsage: true,
}
func runWhoami(cmd *cobra.Command, args []string) error {
session := aws.GetSession()
callerIdentity, err := aws.GetCallerIdentity(session)
if err != nil {
return err
}
var name string
if viper.GetBool("swag.enable") {
name, err = swag.AccountName(*callerIdentity.Account)
if err != nil {
cmd.Printf("Failed to get account info from SWAG: %v\n", err)
}
}
role := roleFromArn(*callerIdentity.Arn)
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "Role:\t%s\n", role)
if name != "" {
fmt.Fprintf(w, "Account:\t%s (%s)\n", name, *callerIdentity.Account)
} else {
fmt.Fprintf(w, "Account:\t%s\n", *callerIdentity.Account)
}
fmt.Fprintf(w, "ARN:\t%s\n", *callerIdentity.Arn)
fmt.Fprintf(w, "UserId:\t%s\n", *callerIdentity.UserId)
w.Flush()
return nil
}
func roleFromArn(arn string) string {
parts := strings.Split(arn, "/")
return parts[1]
}