Skip to content

Commit 9dc01a6

Browse files
authored
feat(whoami): add -w flag to print current account ID + name (#28)
1 parent aa48c7a commit 9dc01a6

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

main.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2409,11 +2409,90 @@ func handleInteractiveSession() {
24092409
os.Exit(0)
24102410
}
24112411

2412+
func handleWhoami() {
2413+
configMgr, err := config.NewManager()
2414+
2415+
if err != nil {
2416+
fatalError(fmt.Sprintf("Error initializing config manager: %v", err), "")
2417+
}
2418+
2419+
lastSSOProfileName, err := configMgr.GetLastSelectedSSOProfile()
2420+
profiles, err := configMgr.LoadProfiles()
2421+
2422+
if err != nil {
2423+
fatalError(fmt.Sprintf("Error loading SSO profiles: %v", err), "")
2424+
}
2425+
2426+
var selectedProfile *config.SSOProfile
2427+
2428+
for i := range profiles {
2429+
if profiles[i].Name == lastSSOProfileName {
2430+
selectedProfile = &profiles[i]
2431+
2432+
break
2433+
}
2434+
}
2435+
2436+
if selectedProfile == nil {
2437+
fatalError(
2438+
fmt.Sprintf("Error: Last used SSO profile '%s' not found in configuration.", lastSSOProfileName),
2439+
"Please check your configuration or run 'sesh' interactively.",
2440+
)
2441+
}
2442+
2443+
cachedToken, err := configMgr.LoadToken(selectedProfile.StartURL)
2444+
2445+
if err != nil {
2446+
fmt.Fprintf(os.Stderr, "Warning: Failed to check token cache: %v\\n", err)
2447+
}
2448+
2449+
if cachedToken == nil {
2450+
fatalError(
2451+
"Error: No active session found for the last used profile. Authentication required.",
2452+
fmt.Sprintf("Please run 'sesh' interactively or 'sesh %s <ACCOUNTNAME>' first.", selectedProfile.Name),
2453+
)
2454+
}
2455+
2456+
awsClient, err := aws.NewClient(selectedProfile.SSORegion)
2457+
2458+
if err != nil {
2459+
fatalError(fmt.Sprintf("Error initializing AWS client: %v", err), "")
2460+
}
2461+
2462+
var selectedAccountID aws.Account
2463+
2464+
ctx := context.Background()
2465+
accounts, err := awsClient.ListAccounts(ctx, cachedToken.AccessToken, nil)
2466+
lastAccountName, err := configMgr.GetLastSelectedAccount(selectedProfile.Name)
2467+
2468+
for _, acc := range accounts {
2469+
if acc.Name == lastAccountName {
2470+
selectedAccountID = acc
2471+
2472+
break
2473+
}
2474+
}
2475+
2476+
cliStyles := getDynamicStyles(true)
2477+
2478+
fmt.Println(
2479+
lipgloss.JoinHorizontal(lipgloss.Left,
2480+
cliStyles.text.Render(selectedProfile.Name),
2481+
cliStyles.text.Render(" / "),
2482+
cliStyles.primary.Render(selectedAccountID.Name),
2483+
cliStyles.text.Render(" / "),
2484+
cliStyles.secondary.Render(selectedAccountID.AccountID),
2485+
),
2486+
)
2487+
os.Exit(0)
2488+
}
2489+
24122490
func main() {
24132491
// Define flags
24142492
browserFlag := flag.BoolP("browser", "b", false, "Open AWS console in browser instead of setting credentials")
24152493
regionFlag := flag.StringP("region", "r", "", "Specify the AWS region to use")
24162494
versionFlag := flag.BoolP("version", "v", false, "Print version information")
2495+
whoamiFlag := flag.BoolP("whoami", "w", false, "Print current AWS Account name & ID")
24172496

24182497
flag.Parse()
24192498

@@ -2425,7 +2504,7 @@ func main() {
24252504

24262505
args := flag.Args()
24272506

2428-
usageString := "Usage: sesh [options] [SSONAME ACCOUNTNAME [ROLENAME]]\nOptions:\n --version, -v Print version information\n --browser, -b Open AWS console in browser\n --region, -r REGION Specify AWS region"
2507+
usageString := "Usage: sesh [options] [SSONAME ACCOUNTNAME [ROLENAME]]\nOptions:\n --version, -v Print version information\n --browser, -b Open AWS console in browser\n --region, -r REGION Specify AWS region\n --whoami, -w Print current AWS Account name & ID"
24292508

24302509
// Check for direct session setup (2 or 3 args)
24312510
if len(args) == 2 || len(args) == 3 {
@@ -2448,6 +2527,8 @@ func main() {
24482527
if *browserFlag {
24492528
// Handle opening last session in browser
24502529
handleLastSessionBrowser()
2530+
} else if *whoamiFlag {
2531+
handleWhoami()
24512532
} else {
24522533
// Start interactive TUI
24532534
handleInteractiveSession()

0 commit comments

Comments
 (0)