Skip to content

Commit 416ad5a

Browse files
committed
Fixed issue with tab segfaulting and hoisted errors to top of file
1 parent 62c994a commit 416ad5a

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

internal/clean.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111

1212
var (
1313
cleanAll = false
14-
allowedCleanArgs = []string{"creds", "sso"}
14+
allowedCleanArgs = []string{"cached", "sso"}
1515
)
1616

1717
var cleanCmd = &cobra.Command{
1818
Use: "clean [" + strings.Join(allowedCleanArgs, "] [") + "]",
19-
Short: "Clean sso and role credentials from cache",
19+
Short: "Clean sso client credentials and role credentials from cache",
2020
Args: cobra.RangeArgs(1, 2),
2121
ValidArgs: allowedCleanArgs,
22-
Example: " knox clean creds\n knox clean sso -a\n knox clean creds sso",
22+
Example: " knox clean cached\n knox clean sso -a\n knox clean cached sso",
2323
Run: func(cmd *cobra.Command, args []string) {
24-
if slices.Contains(args, "creds") {
24+
if slices.Contains(args, "cached") {
2525
roles, err := credentials.GetSavedRolesWithCredentials()
2626
if err != nil {
2727
ExitWithError(1, "failed to get role credentials", err)
@@ -65,5 +65,5 @@ var cleanCmd = &cobra.Command{
6565
func init() {
6666
RootCmd.AddCommand(cleanCmd)
6767
cleanCmd.Flags().SortFlags = true
68-
cleanCmd.Flags().BoolVarP(&cleanAll, "all", "a", cleanAll, "Delete even if not expired credentials")
68+
cleanCmd.Flags().BoolVarP(&cleanAll, "all", "a", cleanAll, "Delete even if not expired")
6969
}

internal/root.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ func SelectRoleCredentialsStartingFromCache() (string, *credentials.Role) {
125125
var sessions credentials.Sessions
126126
var session *credentials.Session
127127
var role *credentials.Role
128-
role, action, err = tui.SelectRolesCredentials()
129-
if action != "" {
130-
return action, nil
131-
}
132-
if err != nil {
128+
if role, action, err = tui.SelectRolesCredentials(); err != nil {
133129
ExitWithError(1, "failed to pick a role", err)
130+
} else if action != "" {
131+
return action, nil
134132
}
135133
if role.Credentials == nil || role.Credentials.IsExpired() {
136134
if sessions, err = credentials.GetSessions(); err != nil {

sdk/credentials/session.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import (
2020
"gopkg.in/ini.v1"
2121
)
2222

23+
var (
24+
ErrorRoleCredentialsNil = fmt.Errorf("role credentials are nil")
25+
ErrRoleNil = fmt.Errorf("role cannot be nil")
26+
)
27+
2328
type Instances []Instance
2429

2530
type Instance struct {
@@ -74,7 +79,7 @@ func (r Roles) FindByName(name string) *Role {
7479
func (r *Role) GetManagedInstances() (Instances, error) {
7580
instances := Instances{}
7681
if r.Credentials == nil {
77-
return instances, fmt.Errorf("role credentials are nil")
82+
return instances, ErrorRoleCredentialsNil
7883
}
7984
staticProvider := awscredentials.NewStaticCredentialsProvider(
8085
r.Credentials.AccessKeyId,
@@ -376,7 +381,7 @@ func (s *Session) GetRoles(accountId string) (Roles, error) {
376381

377382
func (s *Session) RefreshRoleCredentials(role *Role) error {
378383
if role == nil {
379-
return fmt.Errorf("role cannot be nil")
384+
return ErrRoleNil
380385
}
381386
options := sso.Options{Region: s.Region}
382387
client := sso.New(options)

sdk/picker/picker.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,8 @@ func (p *picker) Pick() (*option, *keys.KeyCode) {
191191
defer ansi.ClearDown()
192192
defer ansi.ShowCursor()
193193
p.render()
194-
var firedKeyCode keys.KeyCode
194+
var firedActionKeyCode * keys.KeyCode
195195
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
196-
for _, action := range p.actions {
197-
if key.Code == action.key {
198-
firedKeyCode = action.key
199-
return true, nil
200-
}
201-
}
202196
if key.Code == keys.CtrlC {
203197
p.selectedIndex = -1
204198
return true, nil
@@ -240,13 +234,19 @@ func (p *picker) Pick() (*option, *keys.KeyCode) {
240234
p.render()
241235
}
242236
}
237+
for _, action := range p.actions {
238+
if key.Code == action.key {
239+
firedActionKeyCode = &action.key
240+
return true, nil
241+
}
242+
}
243243
return false, nil
244244
})
245245
if p.selectedIndex < 0 {
246-
return nil, nil
246+
return nil, firedActionKeyCode
247247
}
248248
if p.selectedIndex >= len(p.filtered) {
249-
return nil, nil
249+
return nil, firedActionKeyCode
250250
}
251-
return p.filtered[p.selectedIndex], &firedKeyCode
251+
return p.filtered[p.selectedIndex], firedActionKeyCode
252252
}

sdk/tui/tui.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import (
1414
)
1515

1616
var (
17-
MaxItemsToShow int = 10
17+
MaxItemsToShow int = 10
18+
ErrNotPickedSession = fmt.Errorf("no sso session picked")
19+
ErrNotPickedAccount = fmt.Errorf("no account picked")
20+
ErrNotPickedRole = fmt.Errorf("no role picked")
21+
ErrNotPickedInstance = fmt.Errorf("no instance picked")
22+
ErrNotPickedRoleCredentials = fmt.Errorf("no role credentials picked")
1823
)
1924

2025
func ClientLogin(session *credentials.Session) error {
@@ -74,7 +79,7 @@ func SelectSession(sessions credentials.Sessions) (string, string, error) {
7479
return "", "toggle-view", nil
7580
}
7681
if selection == nil {
77-
return "", "", fmt.Errorf("no sso session picked")
82+
return "", "", ErrNotPickedSession
7883
}
7984
return selection.Value.(string), "", nil
8085
}
@@ -98,7 +103,7 @@ func SelectAccount(session *credentials.Session) (string, string, error) {
98103
return "", "back", nil
99104
}
100105
if selection == nil {
101-
return "", "", fmt.Errorf("no account picked")
106+
return "", "", ErrNotPickedAccount
102107
}
103108
return selection.Value.(string), "", nil
104109
}
@@ -123,7 +128,7 @@ func SelectRole(roles credentials.Roles) (string, string, error) {
123128
return "", "back", nil
124129
}
125130
if selection == nil {
126-
return "", "", fmt.Errorf("no role picked")
131+
return "", "", ErrNotPickedRole
127132
}
128133
return selection.Value.(string), "", nil
129134
}
@@ -147,7 +152,7 @@ func SelectInstance(role *credentials.Role) (string, string, error) {
147152
return "", "back", nil
148153
}
149154
if selection == nil {
150-
return "", "", fmt.Errorf("no instance picked")
155+
return "", "", ErrNotPickedInstance
151156
}
152157
return selection.Value.(string), "", nil
153158
}
@@ -176,7 +181,7 @@ func SelectRolesCredentials() (*credentials.Role, string, error) {
176181
return nil, "toggle-view", nil
177182
}
178183
if selection == nil {
179-
return nil, "", fmt.Errorf("no role credentials picked")
184+
return nil, "", ErrNotPickedRoleCredentials
180185
}
181186
selected := selection.Value.(credentials.Role)
182187
return &selected, "", nil

0 commit comments

Comments
 (0)