From 4009632deee51cbee2ca0742a0cf608c244d7d20 Mon Sep 17 00:00:00 2001 From: John Pignata Date: Wed, 27 Dec 2017 21:10:08 -0500 Subject: [PATCH] Return useful error if credentials not configured --- cmd/root.go | 17 ++++++++++++++++- console/main.go | 4 ++++ ecs/cluster.go | 8 ++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9f712cc..64f73d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/jpignata/fargate/console" ECS "github.com/jpignata/fargate/ecs" @@ -85,7 +86,21 @@ var rootCmd = &cobra.Command{ ) ecs := ECS.New(sess) - ecs.CreateCluster() + err := ecs.CreateCluster() + + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case "NoCredentialProviders": + console.Issue("Could not find your AWS credentials") + console.Info("Your AWS credentials could not be found. Please configure your environment with your access key") + console.Info(" ID and secret access key using either the shared configuration file or environment variables.") + console.Info(" See http://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials") + console.Info(" for more details.") + console.Exit(1) + default: + console.ErrorExit(err, "Could not create ECS cluster") + } + } if verbose { verbose = true diff --git a/console/main.go b/console/main.go index f2cd2ff..f926407 100644 --- a/console/main.go +++ b/console/main.go @@ -117,6 +117,10 @@ func IssueExit(msg string, a ...interface{}) { os.Exit(1) } +func Exit(code int) { + os.Exit(code) +} + func SetVerbose(verbose bool) { Verbose = verbose } diff --git a/ecs/cluster.go b/ecs/cluster.go index ad5f4b4..93662c9 100644 --- a/ecs/cluster.go +++ b/ecs/cluster.go @@ -8,7 +8,7 @@ import ( const clusterName = "fargate" -func (ecs *ECS) CreateCluster() string { +func (ecs *ECS) CreateCluster() error { console.Debug("Creating ECS cluster") _, err := ecs.svc.CreateCluster( @@ -18,10 +18,10 @@ func (ecs *ECS) CreateCluster() string { ) if err != nil { - console.ErrorExit(err, "Couldn't create ECS cluster") + return err } - console.Debug("Created ECS cluster [%s]", clusterName) + console.Debug("Created ECS cluster %s", clusterName) - return clusterName + return nil }