Skip to content

Commit 269e87b

Browse files
committed
Sort output
1 parent 0af69c6 commit 269e87b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

bin/aws-profiles

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import configparser
33
import os
44
import sys
55

6-
# script.py - List AWS profile names with account IDs from aws_account_id or extracted from role_arn
6+
# script.py - List AWS profile names with account IDs from aws_account_id or extracted from role_arn, sorted by profile name
77

88
# Get patterns from command-line arguments
99
patterns = sys.argv[1:]
@@ -28,13 +28,25 @@ def get_account_id_from_arn(arn):
2828
return parts[4] # The account ID is the 5th element in the ARN
2929
return 'Unknown'
3030

31-
# Iterate through the sections and print those that match the patterns along with the account ID
31+
# Collect profile names and account IDs
32+
profiles_with_ids = []
33+
34+
# Iterate through the sections and collect profile names and account IDs
3235
for section in config.sections():
3336
if not patterns or any(pattern in section for pattern in patterns):
34-
account_id = config.get(section, 'aws_account_id') if config.has_option(section, 'aws_account_id') else 'Unknown'
37+
account_id = (config.get(section, 'aws_account_id')
38+
if config.has_option(section, 'aws_account_id')
39+
else 'Unknown')
3540
# If aws_account_id is not found, try to extract it from role_arn
3641
if account_id == 'Unknown' and config.has_option(section, 'role_arn'):
3742
role_arn = config.get(section, 'role_arn')
3843
account_id = get_account_id_from_arn(role_arn)
3944

40-
print(f"{section}\t{account_id}")
45+
profiles_with_ids.append((section, account_id))
46+
47+
# Sort the list by profile name
48+
sorted_profiles = sorted(profiles_with_ids)
49+
50+
# Print sorted profiles and account IDs
51+
for profile, account_id in sorted_profiles:
52+
print(f"{profile}\t{account_id}")

0 commit comments

Comments
 (0)