Skip to content

Commit

Permalink
Added option to use a JSON file to pass all CLI options (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <>
  • Loading branch information
thim81 authored Sep 18, 2021
1 parent c0940da commit 766efec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.7.0...HEAD)

### Added

- Added option to use a file to pass all CLI options.

## [1.7.0] - 2020-09-01

### Added
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ 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
```

## Examples

A collection of Postman examples are located under `example`.
Expand Down
27 changes: 23 additions & 4 deletions bin/postman-to-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,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 @@ -37,7 +38,7 @@ program
.option('--oauth1-version <value>', 'OAuth1 version.')
.option('--oauth1-realm <value>', 'OAuth1 realm.')
.option('-s, --separate', 'Generate a separate file for each request.')
.option('--k6-handle-summary-json <path>', 'Output the K6 handle summary as a JSON file.')
.option('--k6-handle-summary-json <path>','Output the K6 handle summary as a JSON file.')
.action(run)
.parse(process.argv);

Expand All @@ -46,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'));
} 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 Expand Up @@ -106,7 +125,7 @@ function translateOptions(options) {
post: options.skipPost,
},
k6HandleSummary: {
json: options.k6HandleSummaryJson
json: options.k6HandleSummaryJson,
},
};
}
Expand Down

0 comments on commit 766efec

Please sign in to comment.