Skip to content

Commit

Permalink
Return useful error if credentials not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
jpignata committed Dec 28, 2017
1 parent 1de573e commit 4009632
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 16 additions & 1 deletion cmd/root.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions console/main.go
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions ecs/cluster.go
Expand Up @@ -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(
Expand All @@ -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
}

0 comments on commit 4009632

Please sign in to comment.