Skip to content

Commit

Permalink
sort SSO profiles alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
n3s7or committed Aug 12, 2024
1 parent 617262f commit 45518f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions awscfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package awsconfigfile

import (
"bytes"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -82,6 +83,12 @@ func Merge(opts MergeOpts) error {
opts.SectionNameTemplate = "{{ .AccountName }}/{{ .RoleName }}"
}

sort.SliceStable(opts.Profiles, func(i, j int) bool {
profileNameI := opts.Prefix + opts.Profiles[i].RoleName
profileNameJ := opts.Prefix + opts.Profiles[j].RoleName
return profileNameI < profileNameJ
})

funcMap := sprig.TxtFuncMap()
sectionNameTempl, err := template.New("").Funcs(funcMap).Parse(opts.SectionNameTemplate)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,48 @@ granted_sso_role_name = DevRole
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile prod/DevRole
region = us-west-2
`,
},
{
name: "sso profiles sorted alphabetically",
profiles: []SSOProfile{
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "prod",
RoleName: "DevRoleTwo",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "prod",
RoleName: "DevRoleOne",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
},
want: `
[profile prod/DevRoleOne]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleOne
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile prod/DevRoleOne
region = us-west-2
[profile prod/DevRoleTwo]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleTwo
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile prod/DevRoleTwo
region = us-west-2
`,
},
}
Expand Down

0 comments on commit 45518f2

Please sign in to comment.