From dbc7addb39437922a8dcfaa4e7177abfc0ffa16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20D=C3=ADaz?= Date: Tue, 22 May 2018 12:08:32 -0600 Subject: [PATCH] Added flags for quiet and as-json output. (#26) --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`. --- bin/cli/base.js | 2 +- bin/cli/commands/request.js | 66 ++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/bin/cli/base.js b/bin/cli/base.js index dae23b5..2ac7744 100644 --- a/bin/cli/base.js +++ b/bin/cli/base.js @@ -41,7 +41,7 @@ Base.flags = { }), verbose: flags.boolean({ char: 'V', - description: 'Show all additional information available for a command.' + description: `Show all additional information available for a command.` }), 'no-format': flags.boolean({ description: `Disables color formatting for usage on external tools.` diff --git a/bin/cli/commands/request.js b/bin/cli/commands/request.js index 5989e63..d938f7c 100644 --- a/bin/cli/commands/request.js +++ b/bin/cli/commands/request.js @@ -38,7 +38,9 @@ class RequestCommand extends Base { param: params, config, 'no-format': noFormat = false, - verbose = false + verbose = false, + 'as-json': asJson = false, + quiet = false }, args } = this.parse(RequestCommand); @@ -51,26 +53,43 @@ class RequestCommand extends Base { spinnerSprite ); - try { - if (!noFormat) { - this.spinner.start(); - } + let spinnerEnabled = !noFormat && !asJson && !quiet; - let res = await Beau.requests.execByAlias(args.alias); - - 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); + if (spinnerEnabled) { + this.spinner.start(); } + + 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, default: [], 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.` }) };