From 1711334e74acd0259fa52f588d3a4cbb1b5c54b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Sun, 11 Dec 2022 19:49:22 +0000 Subject: [PATCH] cli: lrcli prepared for testing (ref. #66) --- cli/lrcli.js | 187 ++++++++++++++++++++++++++------------------------- 1 file changed, 95 insertions(+), 92 deletions(-) diff --git a/cli/lrcli.js b/cli/lrcli.js index ffd5a22..b1e882e 100755 --- a/cli/lrcli.js +++ b/cli/lrcli.js @@ -4,28 +4,7 @@ import { parse } from "https://deno.land/std/flags/mod.ts"; -var parsed_args = parse( - Deno.args, - { - default: { - h: false, - }, - stopEarly: true, - boolean: [ "h" ], - string: [], - alias: { - h: [ "help" ] - }, - collect: [], - negatable: [], - // a function which is invoked with a command line parameter not defined - // in the options configuration object. If the function returns false, - // the unknown option is not added to parsedArgs. - unknown: null - } -); - -let printUsage = () => { +let getUsage = () => { let usage = ` Command-line interface for LibResilient. @@ -42,11 +21,11 @@ Options: ` - console.log(usage) + return usage } -let printPluginActionUsage = (action, action_name) => { +let getPluginActionUsage = (action, action_name) => { // initialize let options = "" @@ -83,11 +62,11 @@ let printPluginActionUsage = (action, action_name) => { ${action.description}${positional_desc} ${options}` - console.log(usage) + return usage } -let printpluginUsage = (plugin) => { +let getPluginUsage = (plugin) => { let usage = ` CLI plugin: ${plugin.name} @@ -105,17 +84,15 @@ General Options: If plugin-name is provided, print usage information of that plugin. Actions and Action Options: + ` - - console.log(usage) - for (const action in plugin.actions) { - printPluginActionUsage(plugin.actions[action], action) + usage += getPluginActionUsage(plugin.actions[action], action) + '\n' } + + return usage } -//console.log(parsed_args) - let parsePluginActionArgs = (args, argdef) => { var plugin_args_config = { @@ -150,9 +127,7 @@ let parsePluginActionArgs = (args, argdef) => { } } - //console.log(plugin_args_config) var parsed = parse(args, plugin_args_config) - //console.log(parsed) var result = [] @@ -176,70 +151,98 @@ let parsePluginActionArgs = (args, argdef) => { // we *always* pass arguments to plugins as arrays of strings, // even if we only got one value -// no unknown parsed args? that means we have no plugin specified -if (parsed_args._.length == 0) { - printUsage() - Deno.exit(1) -} - -// try loading the plugin -let plugin -try { - plugin = await import(`../plugins/${parsed_args._[0]}/cli.js`); -} catch (e) { - // unable to load the plugin? bail with info - console.log(`\n*** ${e} ***`) - printUsage() - Deno.exit(2) -} - -// if we only had exactly one unknown arg, we only have the plugin name -// but no info from the user what to do with it -// → print plugin usage and exit -if (parsed_args._.length == 1) { - console.log('\n*** No action specified for plugin ***') - printpluginUsage(plugin) - Deno.exit(3) -} - -let action = parsed_args._[1] -if ( ! (action in plugin.actions) ) { - var exit_code = 0 - if (!['--help', '-h'].includes(action)) { - console.log(`\n*** Action not supported: ${action} ***`) - exit_code = 4 +let main = async () => { + + var parsed_args = parse( + Deno.args, + { + default: { + h: false, + }, + stopEarly: true, + boolean: [ "h" ], + string: [], + alias: { + h: [ "help" ] + }, + collect: [], + negatable: [], + // a function which is invoked with a command line parameter not defined + // in the options configuration object. If the function returns false, + // the unknown option is not added to parsedArgs. + unknown: null + } + ); + + // no unknown parsed args? that means we have no plugin specified + if (parsed_args._.length == 0) { + console.log(getUsage()) + return 1 } - printpluginUsage(plugin) - Deno.exit(exit_code) -} -if (['--help', '-h'].includes(parsed_args._[2])) { - printpluginUsage(plugin) - Deno.exit(0) -} + // try loading the plugin + let plugin + try { + plugin = await import(`../plugins/${parsed_args._[0]}/cli.js`); + } catch (e) { + // unable to load the plugin? bail with info + console.log(`\n*** ${e} ***`) + console.log(getUsage()) + return 2 + } -var parsed_plugin_args = parsePluginActionArgs( - // removing the plugin name and the method name - parsed_args._.slice(2), - // empty object in case arguments key does not exist - plugin.actions[action].arguments || {} - ) + // if we only had exactly one unknown arg, we only have the plugin name + // but no info from the user what to do with it + // → print plugin usage and exit + if (parsed_args._.length == 1) { + console.log('\n*** No action specified for plugin ***') + console.log(getPluginUsage(plugin)) + return 3 + } + + let action = parsed_args._[1] + if ( ! (action in plugin.actions) ) { + var exit_code = 0 + if (!['--help', '-h'].includes(action)) { + console.log(`\n*** Action not supported: ${action} ***`) + exit_code = 4 + } + console.log(getPluginUsage(plugin)) + return exit_code + } -//console.log(parsed_plugin_args) + if (['--help', '-h'].includes(parsed_args._[2])) { + console.log(getPluginUsage(plugin)) + return 0 + } -// not using console.log here because we want the *exact* output -// without any extra ending newlines -try { - await Deno.stdout.write( - new TextEncoder().encode( - await plugin.actions[action].run(...parsed_plugin_args) + var parsed_plugin_args = parsePluginActionArgs( + // removing the plugin name and the method name + parsed_args._.slice(2), + // empty object in case arguments key does not exist + plugin.actions[action].arguments || {} + ) + + // not using console.log here because we want the *exact* output + // without any extra ending newlines + try { + await Deno.stdout.write( + new TextEncoder().encode( + await plugin.actions[action].run(...parsed_plugin_args) + ) ) - ) -} catch (e) { - console.log(`\n*** ${e} ***`) - printpluginUsage(plugin) + } catch (e) { + console.log(`\n*** ${e} ***`) + console.log(getPluginUsage(plugin)) + } } +// export the main function +export { + main +} - -//console.log(new URL('', import.meta.url).toString().split('/').at(-1)) +// run only if we're the main module +if (import.meta.main) { + Deno.exit(await main()) +}