Skip to content

Commit 415bbf3

Browse files
author
Chris Gilmer
committed
Allow exiting profiles to be updated with this tool
1 parent 5f21c23 commit 415bbf3

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

cmd/setup.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,21 @@ func (sc *SetupConfig) AddVaultProfile() error {
433433
return nil
434434
}
435435

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
437437
func (sc *SetupConfig) UpdateAWSProfile(iniFile *ini.File, profile, sourceProfile *vault.ProfileSection) error {
438438
sc.Logger.Printf("Adding the profile %s to the AWS config file", profile.Name)
439439

440440
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+
}
444451
}
445452

446453
// Add the source profile when provided
@@ -471,13 +478,11 @@ func (sc *SetupConfig) UpdateAWSConfigFile() error {
471478
return fmt.Errorf("unable to load aws config file: %w", err)
472479
}
473480
// 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 {
476482
return fmt.Errorf("could not add base profile: %w", err)
477483
}
478484
// 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 {
481486
return fmt.Errorf("could not add role profile: %w", err)
482487
}
483488

0 commit comments

Comments
 (0)