@@ -433,14 +433,21 @@ func (sc *SetupConfig) AddVaultProfile() error {
433
433
return nil
434
434
}
435
435
436
- // UpdateAWSProfile adds a single AWS profile to the AWS config file
436
+ // UpdateAWSProfile updates or creates a single AWS profile to the AWS config file
437
437
func (sc * SetupConfig ) UpdateAWSProfile (iniFile * ini.File , profile , sourceProfile * vault.ProfileSection ) error {
438
438
sc .Logger .Printf ("Adding the profile %s to the AWS config file" , profile .Name )
439
439
440
440
sectionName := fmt .Sprintf ("profile %s" , profile .Name )
441
- section , err := iniFile .NewSection (sectionName )
442
- if err != nil {
443
- return fmt .Errorf ("error creating section %q: %w" , profile .Name , err )
441
+
442
+ // Get or create section before updating
443
+ var err error
444
+ var section * ini.Section
445
+ section = iniFile .Section (sectionName )
446
+ if section == nil {
447
+ section , err = iniFile .NewSection (sectionName )
448
+ if err != nil {
449
+ return fmt .Errorf ("error creating section %q: %w" , profile .Name , err )
450
+ }
444
451
}
445
452
446
453
// Add the source profile when provided
@@ -471,13 +478,11 @@ func (sc *SetupConfig) UpdateAWSConfigFile() error {
471
478
return fmt .Errorf ("unable to load aws config file: %w" , err )
472
479
}
473
480
// Add the base profile
474
- err = sc .UpdateAWSProfile (iniFile , sc .BaseProfile , nil )
475
- if err != nil {
481
+ if err = sc .UpdateAWSProfile (iniFile , sc .BaseProfile , nil ); err != nil {
476
482
return fmt .Errorf ("could not add base profile: %w" , err )
477
483
}
478
484
// Add the role profile with base as the source profile
479
- err = sc .UpdateAWSProfile (iniFile , sc .RoleProfile , sc .BaseProfile )
480
- if err != nil {
485
+ if err = sc .UpdateAWSProfile (iniFile , sc .RoleProfile , sc .BaseProfile ); err != nil {
481
486
return fmt .Errorf ("could not add role profile: %w" , err )
482
487
}
483
488
0 commit comments