Skip to content

Commit

Permalink
Moved error handling to common
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Apr 21, 2018
1 parent 6eab77b commit 59005cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
20 changes: 20 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package common

import (
"log"
"os"
)

var (
ErrorLog = log.New(os.Stderr, "", log.LstdFlags)
)

func FatalOnError(err error) {
if err != nil {
ErrorLog.Fatalln(err)
}
}

func Fatalln(message string) {
ErrorLog.Fatalln(message)
}
37 changes: 14 additions & 23 deletions iam/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ func main() {
kingpin.Parse()

if len(*flags.RoleArn) == 0 && len(*saveProfileName) != 0 {
fmt.Println("--save-profile can only be used with --assume-role-arn")
os.Exit(1)
common.Fatalln("--save-profile can only be used with --assume-role-arn")
}

if len(*command) == 0 && len(*saveProfileName) == 0 {
fmt.Println("Use at least one of command or --save-profile-name")
os.Exit(1)
common.Fatalln("Use at least one of command or --save-profile-name")
}

session := session.Must(session.NewSession())
conf := common.AssumeRoleConfig(flags, session)

stsClient := sts.New(session, conf)
res, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
checkNoError(err)
common.FatalOnError(err)

if !*quiet {
fmt.Println(res)
Expand All @@ -52,7 +50,7 @@ func main() {
var creds credentials.Value
if conf.Credentials != nil {
creds, err = conf.Credentials.Get()
checkNoError(err)
common.FatalOnError(err)
}

if len(*saveProfileName) != 0 {
Expand All @@ -68,7 +66,7 @@ func saveProfile(conf *aws.Config, creds *credentials.Value) {
// update the credentials file
credsFilename := os.ExpandEnv("$HOME/.aws/credentials")
credsCfg, err := ini.Load(credsFilename)
checkNoError(err)
common.FatalOnError(err)

_, err = credsCfg.GetSection(*saveProfileName)
if err == nil {
Expand All @@ -87,18 +85,18 @@ func saveProfile(conf *aws.Config, creds *credentials.Value) {
}

newCredsSection, err := credsCfg.NewSection(*saveProfileName)
checkNoError(err)
common.FatalOnError(err)
_, err = newCredsSection.NewKey("aws_access_key_id", creds.AccessKeyID)
checkNoError(err)
common.FatalOnError(err)
_, err = newCredsSection.NewKey("aws_secret_access_key", creds.SecretAccessKey)
checkNoError(err)
common.FatalOnError(err)
_, err = newCredsSection.NewKey("aws_session_token", creds.SessionToken)
checkNoError(err)
common.FatalOnError(err)

// update the config file
configFilename := os.ExpandEnv("$HOME/.aws/config")
configCfg, err := ini.Load(configFilename)
checkNoError(err)
common.FatalOnError(err)

configSectionName := fmt.Sprintf("profile %s", *saveProfileName)
_, err = configCfg.GetSection(configSectionName)
Expand All @@ -107,11 +105,11 @@ func saveProfile(conf *aws.Config, creds *credentials.Value) {
}

newConfigSection, err := configCfg.NewSection(configSectionName)
checkNoError(err)
common.FatalOnError(err)
_, err = newConfigSection.NewKey("region", *conf.Region)
checkNoError(err)
common.FatalOnError(err)
_, err = newConfigSection.NewKey("format", "json")
checkNoError(err)
common.FatalOnError(err)

credsCfg.SaveTo(credsFilename)
configCfg.SaveTo(configFilename)
Expand All @@ -121,7 +119,7 @@ func promptConfirm(text string) bool {
var response string
fmt.Print(text)
_, err := fmt.Scanln(&response)
checkNoError(err)
common.FatalOnError(err)
fmt.Println()
return response == "y"
}
Expand Down Expand Up @@ -156,10 +154,3 @@ func executeCommand(command *[]string, conf *aws.Config, creds *credentials.Valu
p.Stdout = os.Stdout
p.Run()
}

func checkNoError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit 59005cc

Please sign in to comment.