Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve search experience for commands such as file, export #91

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -53,28 +54,48 @@ 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)
}
}
maxLenS := strconv.Itoa(maxLen)
for _, role := range rolesExtended {
account := role.AccountName
if account == "Unknown" {
account = role.AccountNumber
}
account = fmt.Sprintf("%-"+maxLenS+"s", account)
roles = append(roles, account+"\t"+role.RoleName)
// So users can search <account friendly name> <role> or <role> <account friendly name>
rolesSearch = append(rolesSearch, role.AccountName+role.Arn+role.AccountName)
}

// 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,
Size: 10,
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,
}

_, role, err := prompt.Run()
idx, _, err := prompt.Run()
if err != nil {
return "", err
}

return role, nil
return rolesExtended[idx].Arn, nil
}

func isRunningInTerminal() bool {
Expand Down