From 45518f26868d958f666e5e3d78880aa982d79117 Mon Sep 17 00:00:00 2001 From: n3s7or Date: Sun, 11 Aug 2024 19:17:00 -0700 Subject: [PATCH] sort SSO profiles alphabetically --- awscfg.go | 7 +++++++ generator_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/awscfg.go b/awscfg.go index d4d5cda..6de026c 100644 --- a/awscfg.go +++ b/awscfg.go @@ -4,6 +4,7 @@ package awsconfigfile import ( "bytes" + "sort" "strings" "text/template" @@ -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 { diff --git a/generator_test.go b/generator_test.go index 42c28fd..7663aee 100644 --- a/generator_test.go +++ b/generator_test.go @@ -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 `, }, }