From 8bf92ef59cc018928e228c5c5dd936bd86630607 Mon Sep 17 00:00:00 2001 From: giorgioma Date: Sat, 15 Jan 2022 17:19:29 +0100 Subject: [PATCH 1/3] Allow config to be passaed as an argument not as a file --- packages/openapi-merge-cli/src/index.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/openapi-merge-cli/src/index.ts b/packages/openapi-merge-cli/src/index.ts index 828c77d..9a25764 100644 --- a/packages/openapi-merge-cli/src/index.ts +++ b/packages/openapi-merge-cli/src/index.ts @@ -21,7 +21,8 @@ const program = new Command(); program.version(pjson.version); program - .option('-c, --config ', 'The path to the configuration file for the merge tool.'); + .option('-c, --config ', 'The path to the configuration file for the merge tool.') + .option('-j, --json-config ', 'The path to the configuration file for the merge tool.'); class LogWithMillisDiff { @@ -129,7 +130,25 @@ export async function main(): Promise { 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 { + config = await loadConfiguration(program.config); + } if (typeof config === 'string') { console.error(config); From e4be57224729641be06b49543dba9aef4fdd1c27 Mon Sep 17 00:00:00 2001 From: giorgioma Date: Sat, 15 Jan 2022 17:32:28 +0100 Subject: [PATCH 2/3] Added --json-config documentation --- packages/openapi-merge-cli/README.md | 12 ++++++++++++ packages/openapi-merge-cli/src/index.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/openapi-merge-cli/README.md b/packages/openapi-merge-cli/README.md index 49a1e71..7c0fe21 100644 --- a/packages/openapi-merge-cli/README.md +++ b/packages/openapi-merge-cli/README.md @@ -73,6 +73,18 @@ 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 ``` +or you can pass the configuration as a JSON string: + +```shell +npx openapi-merge-cli --json-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]. diff --git a/packages/openapi-merge-cli/src/index.ts b/packages/openapi-merge-cli/src/index.ts index 9a25764..128f147 100644 --- a/packages/openapi-merge-cli/src/index.ts +++ b/packages/openapi-merge-cli/src/index.ts @@ -22,7 +22,7 @@ program.version(pjson.version); program .option('-c, --config ', 'The path to the configuration file for the merge tool.') - .option('-j, --json-config ', 'The path to the configuration file for the merge tool.'); + .option('-j, --json-config ', 'The configuration as a JSON string to be parsed by the merge tool.'); class LogWithMillisDiff { From a414d805ba13952f0d1f0b55db2ea2a92fe6440a Mon Sep 17 00:00:00 2001 From: giorgioma Date: Sat, 15 Jan 2022 17:55:34 +0100 Subject: [PATCH 3/3] Added --yaml-config to pass config as a yaml string --- packages/openapi-merge-cli/README.md | 12 +++++++++++- packages/openapi-merge-cli/src/index.ts | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/openapi-merge-cli/README.md b/packages/openapi-merge-cli/README.md index 7c0fe21..a4552b8 100644 --- a/packages/openapi-merge-cli/README.md +++ b/packages/openapi-merge-cli/README.md @@ -73,7 +73,7 @@ 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 ``` -or you can pass the configuration as a JSON string: +You can pass the configuration as a JSON string: ```shell npx openapi-merge-cli --json-config '{ @@ -85,6 +85,16 @@ npx openapi-merge-cli --json-config '{ ' ``` +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]. diff --git a/packages/openapi-merge-cli/src/index.ts b/packages/openapi-merge-cli/src/index.ts index 128f147..f0457bb 100644 --- a/packages/openapi-merge-cli/src/index.ts +++ b/packages/openapi-merge-cli/src/index.ts @@ -22,7 +22,8 @@ program.version(pjson.version); program .option('-c, --config ', 'The path to the configuration file for the merge tool.') - .option('-j, --json-config ', 'The configuration as a JSON string to be parsed by the merge tool.'); + .option('-j, --json-config ', 'The configuration as a JSON string to be parsed by the merge tool.') + .option('-y, --yaml-config ', 'The configuration as a YAML string to be parsed by the merge tool.'); class LogWithMillisDiff { @@ -146,6 +147,21 @@ export async function main(): Promise { 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); }