From 9de143613b2e1ca6850c52ff985f4f24ef8bee38 Mon Sep 17 00:00:00 2001 From: Ben Kehoe Date: Thu, 23 Feb 2023 11:46:56 +0000 Subject: [PATCH] v2.6: --json produces JSON for errors --- CHANGELOG.md | 4 ++++ README.md | 4 +++- aws-whoami/main.go | 29 ++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3ba0e9..74e2c90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ `aws-whoami` uses [monotonic versioning](https://github.com/benkehoe/monotonic-versioning-manifesto) since v1.0 across both [the older Python implementation](https://github.com/benkehoe/aws-whoami) (compatibility number 1) and this Go implementation (compatibility number 2). +## v2.6 + +* With `--json`, errors are printed as JSON in the form `{"Error": "The error message"}` + ## v2.5 * Add `--disable-account-alias` flag. diff --git a/README.md b/README.md index 8cc29c3..62e07f7 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ To selectively disable it, you can also set the value to a comma-separated list ## JSON output -The JSON object that is printed when using the `--json` flag always includes the following fields: +The JSON object that is printed when using the `--json` flag always (when successful, see below for errors) includes the following fields: * `Account` * `AccountAliases` (NOTE: this is a list) * `Arn` @@ -78,3 +78,5 @@ For the account root, both the `Type` and `Name` are `"root"`. `SSOPermissionSet` is set if the assumed role name conforms to the format `AWSReservedSSO_{permission-set}_{random-tag}`, otherwise it is `null`. Note that the `AccountAliases` field is an empty list when account alias checking is disabled, not `null`. + +If there is an error, a JSON object is printed with the following structure: `{"Error": "The error message"}` diff --git a/aws-whoami/main.go b/aws-whoami/main.go index 4cdaaef..b0ff258 100644 --- a/aws-whoami/main.go +++ b/aws-whoami/main.go @@ -20,7 +20,6 @@ import ( "errors" "flag" "fmt" - "log" "os" "strings" @@ -32,7 +31,7 @@ import ( "github.com/aws/smithy-go" ) -var Version string = "2.5" +var Version string = "2.6" var DisableAccountAliasEnvVarName = "AWS_WHOAMI_DISABLE_ACCOUNT_ALIAS" type Whoami struct { @@ -251,6 +250,23 @@ func (whoami Whoami) Format() string { return strings.Join(lines, "\n") } +type errorRecord struct { + Error string +} + +func printError(err error, useJson bool) { + if useJson { + bytes, err := json.Marshal(errorRecord{err.Error()}) + if err != nil { + fmt.Fprintln(os.Stderr, "{\"Error\": \"Failed to serialize error\"}") + } else { + fmt.Fprintln(os.Stderr, string(bytes)) + } + } else { + fmt.Fprintln(os.Stderr, err.Error()) + } +} + func main() { profile := flag.String("profile", "", "A config profile to use") useJson := flag.Bool("json", false, "Output as JSON") @@ -265,7 +281,8 @@ func main() { awsConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(*profile)) if err != nil { - log.Fatal(err) // exits + printError(err, *useJson) + os.Exit(1) } whoamiParams := NewWhoamiParams() @@ -278,13 +295,15 @@ func main() { Whoami, err := NewWhoami(awsConfig, whoamiParams) if err != nil { - log.Fatal(err) // exits + printError(err, *useJson) + os.Exit(1) } if *useJson { bytes, err := json.Marshal(Whoami) if err != nil { - log.Fatal(err) // exits + printError(err, *useJson) + os.Exit(1) } fmt.Println(string(bytes)) } else {