@@ -433,44 +433,52 @@ func (sc *SetupConfig) AddVaultProfile() error {
433
433
return nil
434
434
}
435
435
436
- // UpdateAWSConfigFile adds the user's AWS profile to the AWS config file
437
- func (sc * SetupConfig ) UpdateAWSConfigFile () error {
438
- sc .Logger .Printf ("Updating the AWS config file: %s" , sc .Config .Path )
439
- // load the ini file
440
- iniFile , err := ini .Load (sc .Config .Path )
436
+ // UpdateAWSProfile adds a single AWS profile to the AWS config file
437
+ func (sc * SetupConfig ) UpdateAWSProfile (iniFile * ini.File , profile , sourceProfile * vault.ProfileSection ) error {
438
+ sc .Logger .Printf ("Adding the profile %s to the AWS config file" , profile .Name )
439
+
440
+ sectionName := fmt .Sprintf ("profile %s" , profile .Name )
441
+ section , err := iniFile .NewSection (sectionName )
441
442
if err != nil {
442
- return fmt .Errorf ("unable to load aws config file : %w" , err )
443
+ return fmt .Errorf ("error creating section %q : %w" , profile . Name , err )
443
444
}
444
- // add the base profile
445
- baseSectionName := fmt .Sprintf ("profile %s" , sc .BaseProfile .Name )
446
- baseSection , err := iniFile .NewSection (baseSectionName )
447
- if err != nil {
448
- return fmt .Errorf ("error creating section %q: %w" , sc .BaseProfile .Name , err )
445
+
446
+ // Add the source profile when provided
447
+ if sourceProfile != nil {
448
+ _ , err = section .NewKey ("source_profile" , sc .BaseProfile .Name )
449
+ if err != nil {
450
+ return fmt .Errorf ("unable to add source profile: %w" , err )
451
+ }
449
452
}
450
- if err = baseSection .ReflectFrom (& sc .BaseProfile ); err != nil {
453
+
454
+ if err = section .ReflectFrom (& profile ); err != nil {
451
455
return fmt .Errorf ("error mapping profile to ini file: %w" , err )
452
456
}
453
- _ , err = baseSection .NewKey ("output" , sc .Output )
457
+ _ , err = section .NewKey ("output" , sc .Output )
454
458
if err != nil {
455
459
return fmt .Errorf ("unable to add output key: %w" , err )
456
460
}
461
+ return nil
462
+ }
463
+
464
+ // UpdateAWSConfigFile adds the user's AWS profile to the AWS config file
465
+ func (sc * SetupConfig ) UpdateAWSConfigFile () error {
466
+ sc .Logger .Printf ("Updating the AWS config file: %s" , sc .Config .Path )
467
+ // load the ini file
468
+ iniFile , err := ini .Load (sc .Config .Path )
457
469
458
- // add the role profile
459
- roleSectionName := fmt .Sprintf ("profile %s" , sc .RoleProfile .Name )
460
- roleSection , err := iniFile .NewSection (roleSectionName )
461
470
if err != nil {
462
- return fmt .Errorf ("error creating section %q : %w" , sc . RoleProfile . Name , err )
471
+ return fmt .Errorf ("unable to load aws config file : %w" , err )
463
472
}
464
- _ , err = roleSection .NewKey ("source_profile" , sc .BaseProfile .Name )
473
+ // Add the base profile
474
+ err = sc .UpdateAWSProfile (iniFile , sc .BaseProfile , nil )
465
475
if err != nil {
466
- return fmt .Errorf ("unable to add source profile: %w" , err )
476
+ return fmt .Errorf ("could not add base profile: %w" , err )
467
477
}
468
- if err = roleSection .ReflectFrom (& sc .RoleProfile ); err != nil {
469
- return fmt .Errorf ("error mapping profile to ini file: %w" , err )
470
- }
471
- _ , err = roleSection .NewKey ("output" , sc .Output )
478
+ // Add the role profile with base as the source profile
479
+ err = sc .UpdateAWSProfile (iniFile , sc .RoleProfile , sc .BaseProfile )
472
480
if err != nil {
473
- return fmt .Errorf ("unable to add output key : %w" , err )
481
+ return fmt .Errorf ("could not add role profile : %w" , err )
474
482
}
475
483
476
484
// save it back to the aws config path
0 commit comments