Skip to content

Commit 5f21c23

Browse files
author
Chris Gilmer
committed
Abstract out the update of aws profiles in aws config to a more generic function
1 parent 8676f3b commit 5f21c23

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

cmd/setup.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -433,44 +433,52 @@ func (sc *SetupConfig) AddVaultProfile() error {
433433
return nil
434434
}
435435

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)
441442
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)
443444
}
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+
}
449452
}
450-
if err = baseSection.ReflectFrom(&sc.BaseProfile); err != nil {
453+
454+
if err = section.ReflectFrom(&profile); err != nil {
451455
return fmt.Errorf("error mapping profile to ini file: %w", err)
452456
}
453-
_, err = baseSection.NewKey("output", sc.Output)
457+
_, err = section.NewKey("output", sc.Output)
454458
if err != nil {
455459
return fmt.Errorf("unable to add output key: %w", err)
456460
}
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)
457469

458-
// add the role profile
459-
roleSectionName := fmt.Sprintf("profile %s", sc.RoleProfile.Name)
460-
roleSection, err := iniFile.NewSection(roleSectionName)
461470
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)
463472
}
464-
_, err = roleSection.NewKey("source_profile", sc.BaseProfile.Name)
473+
// Add the base profile
474+
err = sc.UpdateAWSProfile(iniFile, sc.BaseProfile, nil)
465475
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)
467477
}
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)
472480
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)
474482
}
475483

476484
// save it back to the aws config path

0 commit comments

Comments
 (0)