diff --git a/ecs/backend.go b/ecs/backend.go index e16e3718..dacafe1c 100644 --- a/ecs/backend.go +++ b/ecs/backend.go @@ -49,7 +49,7 @@ type ContextParams struct { CredsFromEnv bool } -func (c ContextParams) HaveRequiredCredentials() bool { +func (c ContextParams) haveRequiredCredentials() bool { if c.AccessKey == "" || c.SecretKey == "" { return false } @@ -81,8 +81,8 @@ func getEcsAPIService(ecsCtx store.EcsContext) (*ecsAPIService, error) { if ecsCtx.CredentialsFromEnv { creds := getEnvVars() - if !creds.HaveRequiredCredentials() { - return nil, fmt.Errorf(`context requires credentials to be passed as environment variable.`) + if !creds.haveRequiredCredentials() { + return nil, fmt.Errorf(`context requires credentials to be passed as environment variable`) } region = creds.Region profile = creds.Profile diff --git a/ecs/context.go b/ecs/context.go index f773cc60..a23dfc60 100644 --- a/ecs/context.go +++ b/ecs/context.go @@ -125,6 +125,56 @@ func (h contextCreateAWSHelper) createContext(c *ContextParams) (interface{}, st }, description } +func (h contextCreateAWSHelper) selectFromLocalProfile(opts *ContextParams) error { + profilesList, err := getProfiles() + if err != nil { + return err + } + // choose profile + opts.Profile, err = h.chooseProfile(profilesList) + if err != nil { + return err + } + + if opts.Region == "" { + region, isDefinedInProfile, err := getRegion(opts.Profile) + if err != nil { + return err + } + if isDefinedInProfile { + opts.Region = region + } else { + fmt.Println("No region defined in the profile. Choose the region to use.") + opts.Region, err = h.chooseRegion(opts.Region, opts.Profile) + if err != nil { + return err + } + } + } + return nil +} + +func (h contextCreateAWSHelper) createProfileFromCredentials(opts *ContextParams) error { + accessKey, secretKey, err := h.askCredentials() + if err != nil { + return err + } + opts.AccessKey = accessKey + opts.SecretKey = secretKey + // we need a region set -- either read it from profile or prompt user + // prompt for the region to use with this context + opts.Region, err = h.chooseRegion(opts.Region, opts.Profile) + if err != nil { + return err + } + // save as a profile + if opts.Profile == "" { + opts.Profile = opts.Name + } + fmt.Printf("Saving credentials under profile %s\n", opts.Profile) + return h.createProfile(opts.Profile, opts) +} + func (h contextCreateAWSHelper) createContextData(_ context.Context, opts ContextParams) (interface{}, string, error) { if opts.CredsFromEnv { ecsCtx, descr := h.createContext(&opts) @@ -147,51 +197,15 @@ func (h contextCreateAWSHelper) createContextData(_ context.Context, opts Contex switch selected { case 0: opts.CredsFromEnv = true - case 1: - profilesList, err := getProfiles() - if err != nil { - return nil, "", err - } - // choose profile - opts.Profile, err = h.chooseProfile(profilesList) - if err != nil { - return nil, "", err - } + err = h.selectFromLocalProfile(&opts) - if opts.Region == "" { - region, isDefinedInProfile, err := getRegion(opts.Profile) - if isDefinedInProfile { - opts.Region = region - } else { - fmt.Println("No region defined in the profile. Choose the region to use.") - opts.Region, err = h.chooseRegion(opts.Region, opts.Profile) - if err != nil { - return nil, "", err - } - } - } case 2: - accessKey, secretKey, err := h.askCredentials() - if err != nil { - return nil, "", err - } - opts.AccessKey = accessKey - opts.SecretKey = secretKey - // we need a region set -- either read it from profile or prompt user - // prompt for the region to use with this context - opts.Region, err = h.chooseRegion(opts.Region, opts.Profile) - if err != nil { - return nil, "", err - } - // save as a profile - if opts.Profile == "" { - opts.Profile = opts.Name - } - fmt.Printf("Saving credentials under profile %s\n", opts.Profile) - h.createProfile(opts.Profile, &opts) + err = h.createProfileFromCredentials(&opts) + } + if err != nil { + return nil, "", err } - ecsCtx, descr := h.createContext(&opts) return ecsCtx, descr, nil }