kopia lustrzana https://gitlab.com/rysiekpl/libresilient
cli: cleaned up the implementation a bit; improved basic-integrity descriptions
rodzic
b9d7d9f0eb
commit
17a20ba559
107
cli/lrcli.js
107
cli/lrcli.js
|
@ -10,7 +10,7 @@ var parsed_args = parse(
|
|||
boolean: [ "h" ],
|
||||
string: [],
|
||||
alias: {
|
||||
h: [ "help", "usage", "u" ]
|
||||
h: [ "help" ]
|
||||
},
|
||||
collect: [],
|
||||
negatable: [],
|
||||
|
@ -33,7 +33,6 @@ Usage:
|
|||
Options:
|
||||
|
||||
-h, --help [plugin-name]
|
||||
-u, --usage [plugin-name]
|
||||
Print this message, if no plugin-name is given.
|
||||
If plugin-name is provided, print usage information of that plugin.
|
||||
|
||||
|
@ -44,10 +43,11 @@ Options:
|
|||
|
||||
let printpluginUsage = (plugin) => {
|
||||
let usage = `
|
||||
LibResilient CLI for plugin: ${plugin.name}.
|
||||
CLI plugin:
|
||||
${plugin.name}
|
||||
|
||||
Plugin Description:
|
||||
${plugin.description}
|
||||
${plugin.description.replace('\n', '\n ')}
|
||||
|
||||
Usage:
|
||||
${new URL('', import.meta.url).toString().split('/').at(-1)} [general-options] ${plugin.name} [plugin-action [action-options]]
|
||||
|
@ -55,7 +55,6 @@ Usage:
|
|||
General Options:
|
||||
|
||||
-h, --help [plugin-name]
|
||||
-u, --usage [plugin-name]
|
||||
Print this message, if no plugin-name is given.
|
||||
If plugin-name is provided, print usage information of that plugin.
|
||||
|
||||
|
@ -65,6 +64,8 @@ Actions and Action Options:
|
|||
console.log(usage)
|
||||
}
|
||||
|
||||
//console.log(parsed_args)
|
||||
|
||||
let parsePluginActionArgs = (args, argdef) => {
|
||||
|
||||
var plugin_args_config = {
|
||||
|
@ -74,8 +75,8 @@ let parsePluginActionArgs = (args, argdef) => {
|
|||
collect: [],
|
||||
negatable: [],
|
||||
unknown: null,
|
||||
default: {},
|
||||
alias: {}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
|
||||
for (const [argname, argconfig] of Object.entries(argdef)) {
|
||||
|
@ -99,16 +100,21 @@ let parsePluginActionArgs = (args, argdef) => {
|
|||
}
|
||||
}
|
||||
|
||||
//console.log(plugin_args_config)
|
||||
var parsed = parse(args, plugin_args_config)
|
||||
//console.log(parsed)
|
||||
|
||||
var result = []
|
||||
|
||||
// we want to keep the order of arguments
|
||||
// as defined in the plugin cli code
|
||||
for (const argname of Object.keys(argdef)) {
|
||||
if (argname in parsed) {
|
||||
result.push(parsed[argname])
|
||||
}
|
||||
}
|
||||
|
||||
// we're done
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -119,36 +125,65 @@ let parsePluginActionArgs = (args, argdef) => {
|
|||
//
|
||||
// we *always* pass arguments to plugins as arrays of strings,
|
||||
// even if we only got one value
|
||||
let plugin
|
||||
if (parsed_args._.length > 0) {
|
||||
plugin = await import(`../plugins/${parsed_args._[0]}/cli.js`);
|
||||
if (parsed_args._.length > 1) {
|
||||
|
||||
let action = parsed_args._[1]
|
||||
if (action in plugin.actions) {
|
||||
|
||||
var parsed_plugin_args = parsePluginActionArgs(
|
||||
// removing the plugin name and the method name
|
||||
parsed_args._.slice(2),
|
||||
plugin.actions[action].arguments
|
||||
)
|
||||
|
||||
// not using console.log here because we want the *exact* output
|
||||
// without any extra ending newlines
|
||||
await Deno.stdout.write(
|
||||
new TextEncoder().encode(
|
||||
await plugin.actions[action].run(...parsed_plugin_args)
|
||||
)
|
||||
)
|
||||
|
||||
} else {
|
||||
printpluginUsage(plugin)
|
||||
}
|
||||
} else {
|
||||
printpluginUsage(plugin)
|
||||
}
|
||||
} else {
|
||||
|
||||
// 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 = 4
|
||||
if (!['--help', '-h'].includes(action)) {
|
||||
console.log(`\n*** Action not supported: ${action} ***`)
|
||||
exit_code = 0
|
||||
}
|
||||
printpluginUsage(plugin)
|
||||
Deno.exit(exit_code)
|
||||
}
|
||||
|
||||
var parsed_plugin_args = parsePluginActionArgs(
|
||||
// removing the plugin name and the method name
|
||||
parsed_args._.slice(2),
|
||||
plugin.actions[action].arguments
|
||||
)
|
||||
|
||||
//console.log(parsed_plugin_args)
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
//console.log(new URL('', import.meta.url).toString().split('/').at(-1))
|
||||
|
|
|
@ -109,13 +109,13 @@ let getIntegrity = async (paths, algos=["SHA-256"], output="json") => {
|
|||
|
||||
// we need non-emtpy arrays of string in the arguments
|
||||
if (!Array.isArray(paths) || (paths.length == 0)) {
|
||||
throw new Error("Expected non-empty array of strings in the 'paths' argument.")
|
||||
throw new Error("Expected non-empty list of files to generate digests of.")
|
||||
}
|
||||
if (!Array.isArray(algos) || (algos.length == 0)) {
|
||||
throw new Error("Expected non-empty array of strings in the 'algos' argument.")
|
||||
throw new Error("Expected non-empty list of algorithms to use.")
|
||||
}
|
||||
if (!['json', 'text'].includes(output)) {
|
||||
throw new Error("Expected either 'json' or 'text' in the 'output' argument.")
|
||||
throw new Error("Expected either 'json' or 'text' as output type to generate.")
|
||||
}
|
||||
|
||||
var result = {}
|
||||
|
@ -142,7 +142,7 @@ let getIntegrity = async (paths, algos=["SHA-256"], output="json") => {
|
|||
|
||||
// this never changes
|
||||
const pluginName = "basic-integrity"
|
||||
const pluginDescription = "verifying subresource integrity for resources fetched by other plugins"
|
||||
const pluginDescription = "Verifying subresource integrity for resources fetched by other plugins.\nCLI used to generate subresource integrity hashes for provided files."
|
||||
const pluginVersion = 'COMMIT_UNKNOWN'
|
||||
const pluginActions = {
|
||||
"get-integrity": {
|
||||
|
|
Ładowanie…
Reference in New Issue