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

add ProfileFormat to config wizard #607

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions cmd/aws-sso/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func setupWizard(ctx *RunContext, reconfig, addSSO, advanced bool) error {
HistoryLimit: 10,
HistoryMinutes: 1440,
UrlExecCommand: []string{},
ProfileFormat: DEFAULT_PROFILE_FORMAT,
}

s.SSO[instanceName] = &sso.SSOConfig{
Expand All @@ -113,6 +114,8 @@ func setupWizard(ctx *RunContext, reconfig, addSSO, advanced bool) error {
}
}

s.ProfileFormat = promptProfileFormat(s.ProfileFormat)

if advanced {
// first, caching
s.CacheRefresh = promptCacheRefresh(s.CacheRefresh)
Expand Down
63 changes: 61 additions & 2 deletions cmd/aws-sso/config_prompt.go → cmd/aws-sso/config_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"regexp"
"runtime"
"slices"
"strconv"
"strings"

Expand All @@ -34,8 +35,10 @@ import (
)

const (
START_URL_FORMAT = "https://%s/start"
START_FQDN_SUFFIX = ".awsapps.com"
START_URL_FORMAT = "https://%s/start"
START_FQDN_SUFFIX = ".awsapps.com"
NICE_PROFILE_FORMAT = "{{ FirstItem .AccountName (.AccountAlias | nospace) }}:{{ .RoleName }}"
DEFAULT_PROFILE_FORMAT = "{{ .AccountIdPad }}:{{ .RoleName }}"
)

type selectOptions struct {
Expand Down Expand Up @@ -627,6 +630,62 @@ func promptFullTextSearch(flag bool) bool {
return yesNoItems[i].Value == "Yes"
}

func promptProfileFormat(value string) string {
var err error
var i int = -1

items := []selectOptions{
{
Name: fmt.Sprintf("Default:\t%s", DEFAULT_PROFILE_FORMAT),
Value: DEFAULT_PROFILE_FORMAT,
},
{
Name: fmt.Sprintf("Friendly:\t%s", NICE_PROFILE_FORMAT),
Value: NICE_PROFILE_FORMAT,
},
}

hasValue := false
for _, v := range items {
if v.Value == value {
hasValue = true
break
}
}
if !hasValue {
items = slices.Insert(items, 0, selectOptions{
Name: fmt.Sprintf("Custom:\t%s", value),
Value: value,
})
}

idx := 0
for i := range items {
if items[i].Value == value {
idx = 0
break
}
}

fmt.Printf("\n")
label := "ProfileFormat for Profile/$AWS_PROFILE:"
for i < 0 {
sel := promptui.Select{
Label: label,
Items: items,
CursorPos: idx,
HideSelected: false,
Stdout: &utils.BellSkipper{},
Templates: makeSelectTemplate(label),
}
if i, _, err = sel.Run(); err != nil {
checkSelectError(err)
}
}

return items[i].Value
}

func promptCacheRefresh(defaultValue int64) int64 {
var val string
var err error
Expand Down
35 changes: 19 additions & 16 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,6 @@ you should use `brew install -s aws-sso-cli` or `brew upgrade -s aws-sso-cli`.

## Profiles and Tags

### Spaces are invalid in Profile names

If your [ProfileFormat](config.md#ProfileFormat) contains either `AccountName`
or `AccountAlias` you may end up with an invalid Profile name which contains a
space.

Their are two possible solutions:

1. Don't include a space in the [AccountName](config.md#name)
2. Strip/replace the space [via the ProfileFormat option](#how-to-configure-profileformat)

### AccountAlias vs AccountName

Due to poor decisions on my part, this is ugly. Sorry about that. With that
Expand Down Expand Up @@ -308,25 +297,39 @@ Some example `ProfileFormat` values:

* `'{{ FirstItem .AccountName .AccountAlias }}'` -- If there
is an Account Name set in the config.yaml print that, otherwise print the
Account Alias defined by the AWS administrator.
Account Name defined by the AWS account owner.
* `'{{ .AccountIdPad }}'` -- Pad the AccountId with leading zeros if it is < 12 digits long
* `'{{ .AccountId }}'` -- Print the AccountId as a regular number
* `'{{ StringsJoin ":" .AccountAlias .RoleName }}'` -- Another
way of writing `{{ .AccountAlias }}:{{ .RoleName }}`
* `'{{ StringReplace " " "_" .AccountAlias }}'` -- Replace any spaces (` `) in the AccountAlias with an underscore (`_`).
* `'{{ FirstItem .AccountName nospaces(.AccountAlias) }}:{{ .RoleName }}'`
-- Use the Account Name if set, otherwise use the Account Alias (without spaces)
* `'{{ FirstItem .AccountName (nospace .AccountAlias) }}:{{ .RoleName }}'`
-- Use the Account Name if set, otherwise use the Account Alias (without spaces via
[nospace](http://masterminds.github.io/sprig/strings.html#nospace))
and then append a colon, followed by the IAM Role Name.
* `'{{ .AccountAlias | kebabcase }}:{{ .RoleName }}'
* `'{{ kebabcase .AccountAlias }}:{{ .RoleName }}'
-- Reformat the AWS account alias like `AStringLikeThis` into
`a-string-like-this` using the [kebabcase function](
http://masterminds.github.io/sprig/strings.html#kebabcase).

For a full list of available variables, [see here](config.md#profileformat).
For a full list of available variables and functions, see the
[ProfileFormat config option](config.md#profileformat).

To see a list of values across your roles for a given variable, you can use
the [list](index.md#list) command.

### Spaces are invalid in Profile names

If your [ProfileFormat](config.md#ProfileFormat) contains either `AccountName`
or `AccountAlias` you may end up with an invalid Profile name which contains a
space.

Their are two possible solutions:

1. Don't include a space in the [AccountName](config.md#name)
2. Strip/replace the space [via the ProfileFormat option](#how-to-configure-profileformat)


### What are the purpose of the Tags?

Tags are key/value pairs that you can use to search for roles to assume when
Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ functions, but a few custom functions are available:
single-quote (`'`) the value because because `ProfileFormat` values often start
with a `{`.

For more information, [see the FAQ](FAQ.md#how-to-configure-profileformat).
For more information, [see the FAQ](FAQ.md#profiles-and-tags).

#### ConfigVariables

Expand Down