forked from extern/bruno
#199 add CLI feature to use command line parameter '--env-var secret=xzy123'
This commit is contained in:
parent
4531cfc994
commit
74282706aa
@ -1,7 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { exists, isFile, isDirectory, getSubDirectories } = require('../utils/filesystem');
|
const { exists, isFile, isDirectory } = require('../utils/filesystem');
|
||||||
const { runSingleRequest } = require('../runner/run-single-request');
|
const { runSingleRequest } = require('../runner/run-single-request');
|
||||||
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
||||||
const { rpad } = require('../utils/common');
|
const { rpad } = require('../utils/common');
|
||||||
@ -103,8 +103,7 @@ const getBruFilesRecursively = (dir) => {
|
|||||||
return bruJsons;
|
return bruJsons;
|
||||||
};
|
};
|
||||||
|
|
||||||
const bruJsons = getFilesInOrder(dir);
|
return getFilesInOrder(dir);
|
||||||
return bruJsons;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const builder = async (yargs) => {
|
const builder = async (yargs) => {
|
||||||
@ -122,6 +121,10 @@ const builder = async (yargs) => {
|
|||||||
describe: 'Environment variables',
|
describe: 'Environment variables',
|
||||||
type: 'string'
|
type: 'string'
|
||||||
})
|
})
|
||||||
|
.option('env-var', {
|
||||||
|
describe: 'Overwrite a single environment variable, multiple usages possible',
|
||||||
|
type: 'string'
|
||||||
|
})
|
||||||
.option('insecure', {
|
.option('insecure', {
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
description: 'Allow insecure server connections'
|
description: 'Allow insecure server connections'
|
||||||
@ -129,12 +132,16 @@ const builder = async (yargs) => {
|
|||||||
.example('$0 run request.bru', 'Run a request')
|
.example('$0 run request.bru', 'Run a request')
|
||||||
.example('$0 run request.bru --env local', 'Run a request with the environment set to local')
|
.example('$0 run request.bru --env local', 'Run a request with the environment set to local')
|
||||||
.example('$0 run folder', 'Run all requests in a folder')
|
.example('$0 run folder', 'Run all requests in a folder')
|
||||||
.example('$0 run folder -r', 'Run all requests in a folder recursively');
|
.example('$0 run folder -r', 'Run all requests in a folder recursively')
|
||||||
|
.example(
|
||||||
|
'$0 run request.bru --env local --env-var secret=xxx',
|
||||||
|
'Run a request with the environment set to local and overwrite the variable secret with value xxx'
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handler = async function (argv) {
|
const handler = async function (argv) {
|
||||||
try {
|
try {
|
||||||
let { filename, cacert, env, insecure, r: recursive } = argv;
|
let { filename, cacert, env, envVar, insecure, r: recursive } = argv;
|
||||||
const collectionPath = process.cwd();
|
const collectionPath = process.cwd();
|
||||||
|
|
||||||
// todo
|
// todo
|
||||||
@ -175,6 +182,35 @@ const handler = async function (argv) {
|
|||||||
envVars = getEnvVars(envJson);
|
envVars = getEnvVars(envJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (envVar) {
|
||||||
|
if (typeof envVar === 'string') {
|
||||||
|
let parts = envVar.split('=');
|
||||||
|
if (parts.length !== 2) {
|
||||||
|
console.error(
|
||||||
|
chalk.red(`overridable environment variable not correct: use name=value - presented: `) +
|
||||||
|
chalk.dim(`${envVar}`)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
envVars[parts[0]] = parts[1];
|
||||||
|
} else if (typeof envVar === 'object' && Array.isArray(envVar)) {
|
||||||
|
envVar.forEach((value) => {
|
||||||
|
let parts = value.split('=');
|
||||||
|
if (parts.length !== 2) {
|
||||||
|
console.error(
|
||||||
|
chalk.red(`overridable environment variable not correct: use name=value - presented: `) +
|
||||||
|
chalk.dim(`${value}`)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
envVars[parts[0]] = parts[1];
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.error(chalk.red(`overridable environment variables not parsable: use name=value`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const options = getOptions();
|
const options = getOptions();
|
||||||
if (insecure) {
|
if (insecure) {
|
||||||
options['insecure'] = true;
|
options['insecure'] = true;
|
||||||
@ -240,7 +276,7 @@ const handler = async function (argv) {
|
|||||||
} else {
|
} else {
|
||||||
console.log(chalk.yellow('Running Folder Recursively \n'));
|
console.log(chalk.yellow('Running Folder Recursively \n'));
|
||||||
|
|
||||||
bruJsons = await getBruFilesRecursively(filename);
|
bruJsons = getBruFilesRecursively(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
let assertionResults = [];
|
let assertionResults = [];
|
||||||
|
Loading…
Reference in New Issue
Block a user