Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Allow config to be passed as a JSON o YAML string argument - not only as a file #35

Open
wants to merge 4 commits into
base: main
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
22 changes: 22 additions & 0 deletions packages/openapi-merge-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ If you wish, you may write your configuration file in YAML format and then run:
npx openapi-merge-cli --config path/to/openapi-merge.yaml
```

You can pass the configuration as a JSON string:

```shell
npx openapi-merge-cli --json-config '{
"inputs": [
...
],
"output": ...
}
'
```

You can pass the configuration as a YAML string:

```shell
npx openapi-merge-cli --yaml-config '---
inputs:
...
output: ...
'
```

And the merge should be run and complete! Congratulations and enjoy!

If you experience any issues then please [raise them in the bug tracker][1].
Expand Down
39 changes: 37 additions & 2 deletions packages/openapi-merge-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const program = new Command();
program.version(pjson.version);

program
.option('-c, --config <config_file>', 'The path to the configuration file for the merge tool.');
.option('-c, --config <config_file>', 'The path to the configuration file for the merge tool.')
.option('-j, --json-config <json_config>', 'The configuration as a JSON string to be parsed by the merge tool.')
.option('-y, --yaml-config <yaml_config>', 'The configuration as a YAML string to be parsed by the merge tool.');


class LogWithMillisDiff {
Expand Down Expand Up @@ -129,7 +131,40 @@ export async function main(): Promise<void> {
program.parse(process.argv);
logger.log(`## ${process.argv[0]}: Running v${pjson.version}`);

const config = await loadConfiguration(program.config);
var config
if (program.jsonConfig) {
try {
config = JSON.parse(program.jsonConfig);
} catch (e) {
// We need to make sure the code does propagate the error to the console!
if (e instanceof Error) {
config = e.message;
} else if (typeof e === 'string' || e instanceof String) {
// Make sure it's a string not a String
// Not sure i really need this
config = e.toString()
} else {
config = 'There was an error parsing the config'
}
}
} else if (program.yamlConfig) {
try {
config = yaml.load(program.yamlConfig);
} catch (e) {
// We need to make sure the code does propagate the error to the console!
if (e instanceof Error) {
config = e.message;
} else if (typeof e === 'string' || e instanceof String) {
// Make sure it's a string not a String
// Not sure i really need this
config = e.toString()
} else {
config = 'There was an error parsing the config'
}
}
} else {
config = await loadConfiguration(program.config);
}

if (typeof config === 'string') {
console.error(config);
Expand Down