@@ -2409,11 +2409,90 @@ func handleInteractiveSession() {
2409
2409
os .Exit (0 )
2410
2410
}
2411
2411
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
+
2412
2490
func main () {
2413
2491
// Define flags
2414
2492
browserFlag := flag .BoolP ("browser" , "b" , false , "Open AWS console in browser instead of setting credentials" )
2415
2493
regionFlag := flag .StringP ("region" , "r" , "" , "Specify the AWS region to use" )
2416
2494
versionFlag := flag .BoolP ("version" , "v" , false , "Print version information" )
2495
+ whoamiFlag := flag .BoolP ("whoami" , "w" , false , "Print current AWS Account name & ID" )
2417
2496
2418
2497
flag .Parse ()
2419
2498
@@ -2425,7 +2504,7 @@ func main() {
2425
2504
2426
2505
args := flag .Args ()
2427
2506
2428
- usageString := "Usage: sesh [options] [SSONAME ACCOUNTNAME [ROLENAME]]\n Options:\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]]\n Options:\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 "
2429
2508
2430
2509
// Check for direct session setup (2 or 3 args)
2431
2510
if len (args ) == 2 || len (args ) == 3 {
@@ -2448,6 +2527,8 @@ func main() {
2448
2527
if * browserFlag {
2449
2528
// Handle opening last session in browser
2450
2529
handleLastSessionBrowser ()
2530
+ } else if * whoamiFlag {
2531
+ handleWhoami ()
2451
2532
} else {
2452
2533
// Start interactive TUI
2453
2534
handleInteractiveSession ()
0 commit comments