From 4fbad877be90849ac773813072e2ca1410f5e878 Mon Sep 17 00:00:00 2001 From: Jay Dhulia Date: Mon, 13 Sep 2021 10:42:28 -0600 Subject: [PATCH 1/4] Improve search experience for commands such as file, export --- cmd/helpers.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/cmd/helpers.go b/cmd/helpers.go index 57ac838..ab077b1 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -19,6 +19,7 @@ package cmd import ( "fmt" "os" + "strconv" "strings" "github.com/lithammer/fuzzysearch/fuzzy" @@ -53,10 +54,29 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( } // Retrieve the list of roles - roles, err := client.Roles() + rolesExtended, err := client.RolesExtended() if err != nil { return "", err } + var roles []string + var rolesSearch []string + maxLen := 12 + for _, role := range rolesExtended { + if len(role.AccountName) > maxLen { + maxLen = len(role.AccountName) + } + } + + for _, role := range rolesExtended { + account := role.AccountName + if account == "Unknown" { + account = role.AccountNumber + } + account = fmt.Sprintf("%-"+strconv.Itoa(maxLen)+"s", account) + roles = append(roles, account+"\t"+role.RoleName) + // So users can search or + rolesSearch = append(rolesSearch, role.AccountName+role.Arn+role.AccountName) + } // Prompt the user prompt := promptui.Select{ @@ -64,7 +84,9 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( Items: roles, Size: 16, Searcher: func(input string, index int) bool { - return fuzzy.MatchNormalized(strings.ToLower(input), strings.ToLower(roles[index])) + // filter out all spaces + input = strings.ReplaceAll(input, " ", "") + return fuzzy.MatchNormalizedFold(input, rolesSearch[index]) }, StartInSearchMode: true, } From 1d7eac306172ad85cbcdfea7acd3ffc74545c77a Mon Sep 17 00:00:00 2001 From: Jay Dhulia Date: Mon, 13 Sep 2021 15:00:55 -0600 Subject: [PATCH 2/4] Update help prompt and use role ARN --- cmd/helpers.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/helpers.go b/cmd/helpers.go index ab077b1..95b5126 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -80,7 +80,7 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( // Prompt the user prompt := promptui.Select{ - Label: "Select Role", + Label: "You can search for role name or account name/number or a combination of the two, e.g. prod appname", Items: roles, Size: 16, Searcher: func(input string, index int) bool { @@ -90,13 +90,12 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( }, StartInSearchMode: true, } - - _, role, err := prompt.Run() + idx, _, err := prompt.Run() if err != nil { return "", err } - return role, nil + return rolesExtended[idx].Arn, nil } func isRunningInTerminal() bool { From a278b96748cdcce6d1c84beb6530bef518f578ba Mon Sep 17 00:00:00 2001 From: Jay Dhulia Date: Mon, 13 Sep 2021 15:42:15 -0600 Subject: [PATCH 3/4] slight optimization --- cmd/helpers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helpers.go b/cmd/helpers.go index 95b5126..bc565a8 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -66,13 +66,13 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( maxLen = len(role.AccountName) } } - + maxLenS := strconv.Itoa(maxLen) for _, role := range rolesExtended { account := role.AccountName if account == "Unknown" { account = role.AccountNumber } - account = fmt.Sprintf("%-"+strconv.Itoa(maxLen)+"s", account) + account = fmt.Sprintf("%-"+maxLenS+"s", account) roles = append(roles, account+"\t"+role.RoleName) // So users can search or rolesSearch = append(rolesSearch, role.AccountName+role.Arn+role.AccountName) From ead9a00ec7477b9e9a3d89a80d4cb3fd8b39807d Mon Sep 17 00:00:00 2001 From: Jay Dhulia Date: Tue, 14 Sep 2021 09:45:51 -0600 Subject: [PATCH 4/4] Change default size of roles to 10 --- cmd/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/helpers.go b/cmd/helpers.go index bc565a8..1b57b9f 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -82,7 +82,7 @@ func InteractiveRolePrompt(args []string, region string, client *creds.Client) ( prompt := promptui.Select{ Label: "You can search for role name or account name/number or a combination of the two, e.g. prod appname", Items: roles, - Size: 16, + Size: 10, Searcher: func(input string, index int) bool { // filter out all spaces input = strings.ReplaceAll(input, " ", "")