Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2020-10-30 18:50:56 +01:00
parent de0be8650e
commit 3066a1cdad
2 changed files with 51 additions and 35 deletions

View File

@ -18,6 +18,7 @@ package context
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -46,6 +47,9 @@ func createEcsCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Name = args[0]
if opts.CredsFromEnv && opts.Profile != "" {
return fmt.Errorf("--profile and --from-env flags cannot be set at the same time")
}
if localSimulation {
return runCreateLocalSimulation(cmd.Context(), args[0], opts)
}

View File

@ -66,6 +66,53 @@ func newContextCreateHelper() contextCreateAWSHelper {
}
}
func (h contextCreateAWSHelper) createContextData(_ context.Context, opts ContextParams) (interface{}, string, error) {
if opts.CredsFromEnv {
ecsCtx, descr := h.createContext(&opts)
return ecsCtx, descr, nil
}
if opts.Profile != "" {
// check profile exists
profilesList, err := getProfiles()
if err != nil {
return nil, "", err
}
if !contains(profilesList, opts.Profile) {
return nil, "", fmt.Errorf("profile %q not found", opts.Profile)
}
ecsCtx, descr := h.createContext(&opts)
return ecsCtx, descr, nil
}
options := []string{
"An existing AWS profile",
"A new AWS profile",
"AWS environment variables",
}
selected, err := h.user.Select("Create a Docker context using:", options)
if err != nil {
if err == terminal.InterruptErr {
return nil, "", errdefs.ErrCanceled
}
return nil, "", err
}
switch selected {
case 0:
err = h.selectFromLocalProfile(&opts)
case 1:
err = h.createProfileFromCredentials(&opts)
case 2:
opts.CredsFromEnv = true
}
if err != nil {
return nil, "", err
}
ecsCtx, descr := h.createContext(&opts)
return ecsCtx, descr, nil
}
func (h contextCreateAWSHelper) createContext(c *ContextParams) (interface{}, string) {
if c.Profile == "default" {
c.Profile = ""
@ -134,41 +181,6 @@ func (h contextCreateAWSHelper) createProfileFromCredentials(opts *ContextParams
return h.saveRegion(opts.Name, opts.Region)
}
func (h contextCreateAWSHelper) createContextData(_ context.Context, opts ContextParams) (interface{}, string, error) {
if opts.CredsFromEnv {
ecsCtx, descr := h.createContext(&opts)
return ecsCtx, descr, nil
}
options := []string{
"An existing AWS profile",
"A new AWS profile",
"AWS environment variables",
}
selected, err := h.user.Select("Create a Docker context using:", options)
if err != nil {
if err == terminal.InterruptErr {
return nil, "", errdefs.ErrCanceled
}
return nil, "", err
}
switch selected {
case 0:
err = h.selectFromLocalProfile(&opts)
case 1:
err = h.createProfileFromCredentials(&opts)
case 2:
opts.CredsFromEnv = true
}
if err != nil {
return nil, "", err
}
ecsCtx, descr := h.createContext(&opts)
return ecsCtx, descr, nil
}
func (h contextCreateAWSHelper) saveCredentials(profile string, accessKeyID string, secretAccessKey string) error {
p := credentials.SharedCredentialsProvider{Profile: profile}
_, err := p.Retrieve()