From 248f4a922325e25bb8f48d7da6eee56e08c813cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20D=C3=ADaz?= Date: Sun, 6 May 2018 18:26:19 -0600 Subject: [PATCH] Added params as a flag to request. (#20) This allows the use to inject arbitrary values to Beau's environment. --- bin/cli/base.js | 7 ++++-- bin/cli/commands/request.js | 45 ++++++++++++++++++++++++------------- examples/httpbin.yml | 12 +++++----- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/bin/cli/base.js b/bin/cli/base.js index ade38ed..83a6796 100644 --- a/bin/cli/base.js +++ b/bin/cli/base.js @@ -15,11 +15,14 @@ class Base extends Command { return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8')); } - loadConfig(configFile) { + loadConfig(configFile, params = []) { const config = this.openConfigFile(configFile); const env = dotenv.config().parsed || {}; + params = dotenv.parse(params.reduce((a, p) => a + '\n' + p, '')); - return new Beau(config, env); + const envParams = { params: Object.assign(env, params) }; + + return new Beau(config, envParams); } } diff --git a/bin/cli/commands/request.js b/bin/cli/commands/request.js index e22022c..5989e63 100644 --- a/bin/cli/commands/request.js +++ b/bin/cli/commands/request.js @@ -33,33 +33,38 @@ class RequestCommand extends Base { } async run() { - const { flags, args } = this.parse(RequestCommand); - const Beau = this.loadConfig(flags.config); - this.spinner = new Spinner(clc.yellow(`Requesting: ${args.alias}`), [ - '⣾', - '⣽', - '⣻', - '⢿', - '⡿', - '⣟', - '⣯', - '⣷' - ]); + const { + flags: { + param: params, + config, + 'no-format': noFormat = false, + verbose = false + }, + args + } = this.parse(RequestCommand); + + const Beau = this.loadConfig(config, params); + + const spinnerSprite = ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷']; + this.spinner = new Spinner( + clc.yellow(`Requesting: ${args.alias}`), + spinnerSprite + ); try { - if (!flags['no-format']) { + if (!noFormat) { this.spinner.start(); } let res = await Beau.requests.execByAlias(args.alias); - if (flags['no-format']) { + 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, flags.verbose); + this.prettyOutput(res, verbose); } } catch (err) { new Line().output(); @@ -70,7 +75,15 @@ class RequestCommand extends Base { } RequestCommand.description = `Executes a request by name.`; -RequestCommand.flags = { ...Base.flags }; +RequestCommand.flags = { + ...Base.flags, + param: flags.string({ + char: 'P', + multiple: true, + default: [], + description: `Allows you to inject values into the request's environment.` + }) +}; RequestCommand.args = [ { diff --git a/examples/httpbin.yml b/examples/httpbin.yml index 23d8fb0..f4bb22c 100644 --- a/examples/httpbin.yml +++ b/examples/httpbin.yml @@ -2,13 +2,14 @@ version: 1 endpoint: https://httpbin.org/ cookiejar: true +environment: + params: + name: David GET /anything: alias: anything - form: - name: David - params: - hello: World + payload: + name: $env.params.name GET /cookies/set: alias: set-cookies @@ -22,4 +23,5 @@ POST /post: alias: post formdata: file: $[createReadStream('./github.yml')] - + +