Skip to content

Commit

Permalink
v2.6: --json produces JSON for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benkehoe committed Feb 23, 2023
1 parent cbbc898 commit 9de1436
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -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`
Expand All @@ -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"}`
29 changes: 24 additions & 5 deletions aws-whoami/main.go
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"flag"
"fmt"
"log"
"os"
"strings"

Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit 9de1436

Please sign in to comment.