Skip to content

Commit cdf2848

Browse files
committed
Changed --view parameter to --cached
1 parent 11ce855 commit cdf2848

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

internal/connect.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ var connectCmd = &cobra.Command{
1919
Use: "connect",
2020
Short: "Connect to an EC2 instance using session-manager-plugin",
2121
Args: cobra.ExactArgs(0),
22-
PreRunE: func(cmd *cobra.Command, args []string) error {
23-
if view != "session" && view != "cached" {
24-
return fmt.Errorf("view must be either 'session' or 'cached'")
25-
}
26-
return nil
27-
},
2822
Run: func(cmd *cobra.Command, args []string) {
2923
var err error
3024
var role *credentials.Role
3125
var action string
3226
var binaryPath string
3327
for {
34-
if view == "session" {
28+
if !selectCachedFirst {
3529
action, role = SelectRoleCredentialsStartingFromSession()
3630
} else {
3731
action, role = SelectRoleCredentialsStartingFromCache()
@@ -87,6 +81,6 @@ func init() {
8781
connectCmd.Flags().StringVarP(&accountId, "account-id", "a", accountId, "AWS account ID")
8882
connectCmd.Flags().StringVarP(&roleName, "role-name", "r", roleName, "AWS role name")
8983
connectCmd.Flags().StringVarP(&instanceId, "instance-id", "i", instanceId, "EC2 instance ID")
90-
connectCmd.Flags().StringVarP(&view, "view", "v", view, "session or cached")
84+
connectCmd.Flags().BoolVarP(&selectCachedFirst, "cached", "c", selectCachedFirst, "select from cached credentials")
9185
connectCmd.Flags().Uint32VarP(&connectUid, "uid", "u", connectUid, "UID on instance to 'su' to")
9286
}

internal/root.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
)
1515

1616
var (
17-
Version string = "0.0.0"
18-
debug bool = false
19-
view string = "session"
20-
connectUid uint32 = 0
21-
sessionName string
22-
accountId string
23-
roleName string
24-
instanceId string
17+
Version string = "0.0.0"
18+
debug bool = false
19+
selectCachedFirst bool = false
20+
connectUid uint32 = 0
21+
sessionName string
22+
accountId string
23+
roleName string
24+
instanceId string
2525
)
2626

2727
var RootCmd = &cobra.Command{
@@ -31,11 +31,7 @@ var RootCmd = &cobra.Command{
3131
}
3232

3333
func toggleView() {
34-
if view == "session" {
35-
view = "cached"
36-
} else {
37-
view = "session"
38-
}
34+
selectCachedFirst = !selectCachedFirst
3935
}
4036

4137
func goBack() {
@@ -134,7 +130,7 @@ func SelectRoleCredentialsStartingFromCache() (string, *credentials.Role) {
134130
return action, nil
135131
}
136132
if err != nil {
137-
return "toggle-view", nil
133+
ExitWithError(1, "failed to pick a role", err)
138134
}
139135
if role.Credentials == nil || role.Credentials.IsExpired() {
140136
if sessions, err = credentials.GetSessions(); err != nil {
@@ -161,7 +157,7 @@ func SelectRoleCredentialsStartingFromCache() (string, *credentials.Role) {
161157
return "", role
162158
}
163159

164-
func setupConfigFile () {
160+
func setupConfigFile() {
165161
if homeDir, err := os.UserHomeDir(); err == nil {
166162
os.MkdirAll(homeDir+"/.aws/knox", os.FileMode(0700))
167163
}
@@ -170,12 +166,12 @@ func setupConfigFile () {
170166
viper.SetConfigPermissions(os.FileMode(0600))
171167
viper.AddConfigPath("$HOME/.aws/knox")
172168
viper.SetDefault("default_connect_uid", uint32(0))
173-
viper.SetDefault("default_view", "session")
169+
viper.SetDefault("select_cached_first", false)
174170
viper.SetDefault("max_items_to_show", 10)
175171
viper.SafeWriteConfig()
176172
viper.ReadInConfig()
177173
tui.MaxItemsToShow = viper.GetInt("max_items_to_show")
178-
view = viper.GetString("default_view")
174+
selectCachedFirst = viper.GetBool("select_cached_first")
179175
connectUid = viper.GetUint32("default_connect_uid")
180176
}
181177

internal/select.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ var selectCmd = &cobra.Command{
1111
Use: "select",
1212
Short: "Select specific AWS role credentials",
1313
Args: cobra.ExactArgs(0),
14-
PreRunE: func(cmd *cobra.Command, args []string) error {
15-
if view != "session" && view != "cached" {
16-
return fmt.Errorf("view must be either 'session' or 'cached'")
17-
}
18-
return nil
19-
},
2014
Run: func(cmd *cobra.Command, args []string) {
2115
var role *credentials.Role
2216
var action string
2317
for {
24-
if view == "session" {
18+
if !selectCachedFirst {
2519
action, role = SelectRoleCredentialsStartingFromSession()
2620
} else {
2721
action, role = SelectRoleCredentialsStartingFromCache()
@@ -50,5 +44,5 @@ func init() {
5044
selectCmd.Flags().StringVarP(&sessionName, "sso-session", "s", sessionName, "SSO session name")
5145
selectCmd.Flags().StringVarP(&accountId, "account-id", "a", accountId, "AWS account ID")
5246
selectCmd.Flags().StringVarP(&roleName, "role-name", "r", roleName, "AWS role name")
53-
selectCmd.Flags().StringVarP(&view, "view", "v", view, "session or cached")
47+
selectCmd.Flags().BoolVarP(&selectCachedFirst, "cached", "c", selectCachedFirst, "select from cached credentials")
5448
}

0 commit comments

Comments
 (0)