Skip to content

Commit

Permalink
Merge pull request #266 from Cox-Automotive/json-output-clarity
Browse files Browse the repository at this point in the history
Adds clarity to JSON output
  • Loading branch information
LumaC0 authored Sep 26, 2023
2 parents fd5e15a + 119c189 commit 1cb2875
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Thanks for upgrading to the latest version of the ALKS CLI!

* adds optional JSON output for `alks developer accounts` command
* updates JSON output of `alks developer accounts` to be an object indexed by accountId

Have feedback? https://github.com/Cox-Automotive/ALKS-CLI/issues

Expand Down
2 changes: 1 addition & 1 deletion e2e/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,5 @@ setup() {

run alks developer accounts -o json
[ "$status" -eq 0 ]
assert_output --partial "[["
assert_output --partial "]}}"
}
40 changes: 28 additions & 12 deletions src/lib/handlers/alks-developer-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export async function handleAlksDeveloperAccounts(
if (!contains(outputVals, output)) {
errorAndExit(
'The output provided (' +
output +
') is not in the allowed values: ' +
outputVals.join(', ')
output +
') is not in the allowed values: ' +
outputVals.join(', ')
);
}
const outputObj = output == 'json'
? []
: (
new Table({
head: [
clc.white.bold('Account'),
clc.white.bold('Role'),
clc.white.bold('Type'),
],
colWidths: [50, 50, 25],
}));
new Table({
head: [
clc.white.bold('Account'),
clc.white.bold('Role'),
clc.white.bold('Type'),
],
colWidths: [50, 50, 25],
}));

const doExport = options.export;
const accountRegex = getAccountRegex();
Expand Down Expand Up @@ -90,7 +90,23 @@ export async function handleAlksDeveloperAccounts(

if (!doExport) {
if (output == 'json') {
console.log(JSON.stringify(outputObj));

const accountsOutput: Record<string, any> = {}
outputObj.forEach((accountRolePair: string[]) => {
const accountId: string = accountRolePair[0].split('/')[0]

if (!(accountId in accountsOutput)) {
accountsOutput[accountId] = {
accountAlias: accountRolePair[0].split('- ')[1],
roles: [{role: accountRolePair[1], isIamActive: accountRolePair[2] == "IAM"}]
}
} else {
accountsOutput[accountId].roles.push(
{role: accountRolePair[1], isIamActive: accountRolePair[2] == "IAM"}
)
}
})
console.log(JSON.stringify(accountsOutput));
} else {
console.error(clc.white.underline.bold('\nAvailable Accounts'));
console.log(clc.white(outputObj.toString()));
Expand Down

0 comments on commit 1cb2875

Please sign in to comment.