Added flags for quiet and as-json output.

--as-json outputs the response as json, this allows you to use tools
like jq on the output which is nice.

--quiet makes it so no output is printed. I added this in case you are
looping Beau and don't want to see the output of every single request.
Example: `seq 10 | xargs -I{} beau request hi --quiet`.
This commit is contained in:
David Diaz 2018-05-22 11:28:20 -06:00
parent 293dde59c4
commit 139dd55c4e
2 changed files with 48 additions and 20 deletions

View File

@ -41,7 +41,7 @@ Base.flags = {
}), }),
verbose: flags.boolean({ verbose: flags.boolean({
char: 'V', char: 'V',
description: 'Show all additional information available for a command.' description: `Show all additional information available for a command.`
}), }),
'no-format': flags.boolean({ 'no-format': flags.boolean({
description: `Disables color formatting for usage on external tools.` description: `Disables color formatting for usage on external tools.`

View File

@ -38,7 +38,9 @@ class RequestCommand extends Base {
param: params, param: params,
config, config,
'no-format': noFormat = false, 'no-format': noFormat = false,
verbose = false verbose = false,
'as-json': asJson = false,
quiet = false
}, },
args args
} = this.parse(RequestCommand); } = this.parse(RequestCommand);
@ -51,26 +53,43 @@ class RequestCommand extends Base {
spinnerSprite spinnerSprite
); );
try { let spinnerEnabled = !noFormat && !asJson && !quiet;
if (!noFormat) {
this.spinner.start();
}
let res = await Beau.requests.execByAlias(args.alias); if (spinnerEnabled) {
this.spinner.start();
if (noFormat) {
this.log(res.response.status);
this.log(res.request.endpoint);
this.log(JSON.stringify(res.response.headers));
this.log(JSON.stringify(res.response.body));
} else {
this.prettyOutput(res, verbose);
}
} catch (err) {
new Line().output();
this.spinner.stop();
this.error(err.message);
} }
let res;
try {
res = await Beau.requests.execByAlias(args.alias);
} catch (err) {
this.spinner.stop();
if (!quiet) {
this.error(err.message);
}
this.exit(1);
}
if (quiet) {
return;
}
if (asJson) {
return this.log(JSON.stringify(res.response));
}
if (noFormat) {
this.log(res.response.status);
this.log(res.request.endpoint);
this.log(JSON.stringify(res.response.headers));
this.log(JSON.stringify(res.response.body));
return;
}
this.prettyOutput(res, verbose);
} }
} }
@ -82,6 +101,15 @@ RequestCommand.flags = {
multiple: true, multiple: true,
default: [], default: [],
description: `Allows you to inject values into the request's environment.` description: `Allows you to inject values into the request's environment.`
}),
quiet: flags.boolean({
description: `Skips the output.`
}),
'as-json': flags.boolean({
char: 'j',
description: `Outputs the response as json.`
}) })
}; };