Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Added option to use a JSON file to pass all CLI options #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ Skips any post-request scripts during conversion
$ postman-to-k6 collection.json --skip-pre -o k6-script.js
```

### CLI options file

Manage all the CLI options in a separate configuration file and pass them along to the postman-to-k6 command.
To make the CLI usage easier, especially in CI/CD implementations.

All the available CLI options can be used in the config file. By passing the CLI options as parameters, you can overwrite the defined CLI options defined in the file.

| Flag | Verbose | Default |
| ---- | -------------------- | ------- |
| | `--cli-options-file` | false |

```shell
$ postman-to-k6 collection.json --cli-options-file cli-config.json
```

Example of JSON CLI config file

```json
{
"output": "k6-script.js",
"k6-params": "config/k6-params.json",
"environment": "config/envs/team.env.json",
"separate": true
}
```

## Docker Usage

Using the Docker image, you execute the tool as follows:
Expand Down
24 changes: 22 additions & 2 deletions bin/postman-to-k6.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const convertFile = require('../lib/convert/file');
const utils = require('../lib/convert/utils');
const fs = require('fs-extra');
const outputRequests = require('./requests');
const path = require('path');
Expand All @@ -19,9 +20,10 @@ program
.option('-i, --iterations <count>', 'Number of iterations.')
.option('-g, --global <path>', 'JSON export of global variables.')
.option('-e, --environment <path>', 'JSON export of environment.')
.option('--cli-options-file <path>', 'postman-to-k6 CLI options file. Useful for CI/CD integrations.')
.option('-c, --csv <path>', 'CSV data file. Used to fill data variables.')
.option('-j, --json <path>', 'JSON data file. Used to fill data variables.')
.option('--k6-params <path>', 'K6 param options config file. Used to set the K6 params used during HTTP requests.')
.option('--k6-params <path>', 'K6 param options config file. Sets K6 params used during HTTP requests.')
.option('--skip-pre', 'Skips pre-request scripts')
.option('--skip-post', 'Skips post-request scripts')
.option('--oauth1-consumer-key <value>', 'OAuth1 consumer key.')
Expand All @@ -45,9 +47,27 @@ async function run(...args) {
console.error('Provide path to Postman collection');
return;
}
const options = args.pop();
let options = args.pop();
const input = args.shift();

let cliOptions = {};
if (options.cliOptionsFile) {
try {
const cliOptionsFilePath = path.resolve(options.cliOptionsFile);
cliOptions = JSON.parse(await fs.readFile(cliOptionsFilePath, 'utf8'));
cliOptions = utils.keysToCamel(cliOptions);
} catch (err) {
console.error(
'\x1b[31m',
`postman-to-k6 CLI options error - no such file or directory "${options.cliOptionsFile}"`
);
process.exit(1);
}
}

// Merge CLI configuration file with CLI parameters
options = Object.assign({}, cliOptions, options);

// Convert
let main, requests;
try {
Expand Down
41 changes: 41 additions & 0 deletions lib/convert/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function isArray(a) {
return Array.isArray(a);
}

function isObject(o) {
return o === Object(o) && !isArray(o) && typeof o !== 'function';
}

function keysToCamel(o) {
if (isObject(o)) {
const n = {};

Object.keys(o).forEach(k => {
n[toCamel(k)] = keysToCamel(o[k]);
});

return n;
} else if (isArray(o)) {
return o.map(i => {
return keysToCamel(i);
});
}

return o;
}

function toCamel(s) {
return s.replace(/([-_][a-z])/gi, $1 => {
return $1
.toUpperCase()
.replace('-', '')
.replace('_', '');
});
}

Object.assign(exports, {
keysToCamel,
isArray,
isObject,
toCamel,
});