Added params as a flag to request. (#20)

This allows the use to inject arbitrary values to Beau's environment.
This commit is contained in:
Sergio Díaz 2018-05-06 18:26:19 -06:00 committed by GitHub
parent 56d536a509
commit 248f4a9223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 23 deletions

View File

@ -15,11 +15,14 @@ class Base extends Command {
return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8')); return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
} }
loadConfig(configFile) { loadConfig(configFile, params = []) {
const config = this.openConfigFile(configFile); const config = this.openConfigFile(configFile);
const env = dotenv.config().parsed || {}; 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);
} }
} }

View File

@ -33,33 +33,38 @@ class RequestCommand extends Base {
} }
async run() { async run() {
const { flags, args } = this.parse(RequestCommand); const {
const Beau = this.loadConfig(flags.config); flags: {
this.spinner = new Spinner(clc.yellow(`Requesting: ${args.alias}`), [ 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 { try {
if (!flags['no-format']) { if (!noFormat) {
this.spinner.start(); this.spinner.start();
} }
let res = await Beau.requests.execByAlias(args.alias); let res = await Beau.requests.execByAlias(args.alias);
if (flags['no-format']) { if (noFormat) {
this.log(res.response.status); this.log(res.response.status);
this.log(res.request.endpoint); this.log(res.request.endpoint);
this.log(JSON.stringify(res.response.headers)); this.log(JSON.stringify(res.response.headers));
this.log(JSON.stringify(res.response.body)); this.log(JSON.stringify(res.response.body));
} else { } else {
this.prettyOutput(res, flags.verbose); this.prettyOutput(res, verbose);
} }
} catch (err) { } catch (err) {
new Line().output(); new Line().output();
@ -70,7 +75,15 @@ class RequestCommand extends Base {
} }
RequestCommand.description = `Executes a request by name.`; 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 = [ RequestCommand.args = [
{ {

View File

@ -2,13 +2,14 @@ version: 1
endpoint: https://httpbin.org/ endpoint: https://httpbin.org/
cookiejar: true cookiejar: true
environment:
params:
name: David
GET /anything: GET /anything:
alias: anything alias: anything
form: payload:
name: David name: $env.params.name
params:
hello: World
GET /cookies/set: GET /cookies/set:
alias: set-cookies alias: set-cookies
@ -22,4 +23,5 @@ POST /post:
alias: post alias: post
formdata: formdata:
file: $[createReadStream('./github.yml')] file: $[createReadStream('./github.yml')]