-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (135 loc) · 3.39 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"log"
"os"
"github.com/peterbueschel/awsdefault"
"github.com/urfave/cli"
)
func getProfiles(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "list",
Aliases: []string{"ls", "profiles", "available"},
Usage: "Returns all available profiles from the AWS credentials file.",
Action: func(c *cli.Context) error {
names := file.GetProfilesNames()
for _, n := range names {
fmt.Println(n)
}
return nil
},
}
}
func getUsedProfile(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "get",
Aliases: []string{"show", "is", "now", "curr"},
Usage: "Returns the current AWS default profile.",
Action: func(c *cli.Context) error {
n, idx, err := file.GetUsedProfileNameAndIndex()
if err != nil {
if idx == -2 {
fmt.Println(n)
return nil
}
return err
}
fmt.Println(n)
return nil
},
}
}
func unsetDefaultProfile(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "unset",
Aliases: []string{"rm", "stop", "not"},
Usage: "unset the AWS default profile.",
Action: func(c *cli.Context) error {
return file.UnSetDefault()
},
}
}
func setDefaultProfile(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "set",
Aliases: []string{"to", "should", "replace"},
Usage: "Set/replace the AWS default profile to a given profile. Requires a profile name.",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return fmt.Errorf(
"the name of the profile used to become the new default is required",
)
}
return file.SetDefaultTo(c.Args().First())
},
}
}
func getUsedID(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "id",
Aliases: []string{"aws_access_key_id"},
Usage: "Returns the AWS_ACCESS_KEY_ID of the currently used profile",
Action: func(c *cli.Context) error {
id, err := file.GetUsedID()
if err != nil {
return err
}
fmt.Println(id)
return nil
},
}
}
func getUsedKey(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "key",
Aliases: []string{"aws_secret_access_key"},
Usage: "Returns the AWS_SECRET_ACCESS_KEY of the currently used profile",
Action: func(c *cli.Context) error {
k, err := file.GetUsedKey()
if err != nil {
return err
}
fmt.Println(k)
return nil
},
}
}
func printCredential(file *awsdefault.CredentialsFile) *cli.Command {
return &cli.Command{
Name: "export",
Aliases: []string{"envs"},
Usage: "Returns the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY of the currently used profile in form of export commands.",
Action: func(c *cli.Context) error {
id, err := file.GetUsedID()
if err != nil {
return err
}
k, err := file.GetUsedKey()
if err != nil {
return err
}
fmt.Printf("export AWS_ACCESS_KEY_ID=%s\nexport AWS_SECRET_ACCESS_KEY=%s\n", id, k)
return nil
},
}
}
func main() {
file, err := awsdefault.GetCredentialsFile()
if err != nil {
log.Fatalf("[AWSDEFAULT][ERROR] %v.\n", err)
}
app := cli.NewApp()
app.Commands = []cli.Command{
*setDefaultProfile(file),
*unsetDefaultProfile(file),
*getUsedProfile(file),
*getUsedID(file),
*getUsedKey(file),
*printCredential(file),
*getProfiles(file),
}
err = app.Run(os.Args)
if err != nil {
log.Fatalf("[AWSDEFAULT][ERROR] %s.\n", err)
}
}