kopia lustrzana https://gitlab.com/rysiekpl/libresilient
cli: we have sane output for basic-integrity plugin
rodzic
aef901fd73
commit
3dd94f1c75
12
cli/lrcli.js
12
cli/lrcli.js
|
@ -97,7 +97,7 @@ let parsePluginActionArgs = (args, argdef) => {
|
|||
}
|
||||
|
||||
var parsed = parse(args, plugin_args_config)
|
||||
console.log(parsed)
|
||||
//console.log(parsed)
|
||||
|
||||
var result = []
|
||||
for (const argname of Object.keys(argdef)) {
|
||||
|
@ -130,7 +130,13 @@ if (parsed_args._.length > 0) {
|
|||
plugin.actions[action].arguments
|
||||
)
|
||||
|
||||
plugin.actions[action].run(...parsed_plugin_args)
|
||||
// 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)
|
||||
|
@ -142,4 +148,4 @@ if (parsed_args._.length > 0) {
|
|||
printUsage()
|
||||
}
|
||||
|
||||
console.log(new URL('', import.meta.url).toString().split('/').at(-1))
|
||||
//console.log(new URL('', import.meta.url).toString().split('/').at(-1))
|
||||
|
|
|
@ -34,9 +34,11 @@ let getFileIntegrity = async (path, algos) => {
|
|||
);
|
||||
const fileInfo = await file.stat();
|
||||
|
||||
var result = []
|
||||
|
||||
// are we working with a file?
|
||||
if (fileInfo.isFile) {
|
||||
console.log(`+-- reading: ${path}`)
|
||||
//console.log(`+-- reading: ${path}`)
|
||||
|
||||
// initialize
|
||||
var content = new Uint8Array()
|
||||
|
@ -47,12 +49,12 @@ let getFileIntegrity = async (path, algos) => {
|
|||
|
||||
// read the rest, if there is anything to read
|
||||
while (numread !== null) {
|
||||
console.log(` +-- read: ${numread}`)
|
||||
console.log(` +-- length: ${content.length}`)
|
||||
//console.log(` +-- read: ${numread}`)
|
||||
//console.log(` +-- length: ${content.length}`)
|
||||
|
||||
// there has to be a better way...
|
||||
var new_content = new Uint8Array(content.length + numread);
|
||||
console.log(` +-- new length: ${new_content.length}`)
|
||||
//console.log(` +-- new length: ${new_content.length}`)
|
||||
new_content.set(content)
|
||||
if (buf.length === numread) {
|
||||
new_content.set(buf, content.length)
|
||||
|
@ -64,10 +66,16 @@ let getFileIntegrity = async (path, algos) => {
|
|||
// read some more
|
||||
numread = file.readSync(buf);
|
||||
}
|
||||
console.log(' +-- done.')
|
||||
//console.log(' +-- done.')
|
||||
|
||||
var digest = binToBase64(await crypto.subtle.digest(algos[0], content))
|
||||
console.log(digest)
|
||||
//console.log('+-- calculating digests')
|
||||
for (const algo of algos) {
|
||||
//console.log(` +-- ${algo}`)
|
||||
var digest = algo.toLowerCase().replace('-', '') + '-' + binToBase64(await crypto.subtle.digest(algo, content))
|
||||
//console.log(digest)
|
||||
result.push(digest)
|
||||
}
|
||||
//console.log(`+-- file done: ${path}`)
|
||||
|
||||
}
|
||||
// putting this in a try-catch block as the file
|
||||
|
@ -76,6 +84,9 @@ let getFileIntegrity = async (path, algos) => {
|
|||
try {
|
||||
await file.close();
|
||||
} catch (BadResource) {}
|
||||
|
||||
// return the result
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
|
@ -88,7 +99,7 @@ let getFileIntegrity = async (path, algos) => {
|
|||
* paths - array of strings, paths to individual pieces of content
|
||||
* algos - array of algorithms to use to calculate digests (default: "SHA-256")
|
||||
*/
|
||||
let getIntegrity = (paths, algos=["SHA-256"]) => {
|
||||
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)) {
|
||||
|
@ -97,11 +108,24 @@ let getIntegrity = (paths, algos=["SHA-256"]) => {
|
|||
if (!Array.isArray(algos) || (algos.length == 0)) {
|
||||
throw new Error("Expected non-empty array of strings in the 'algos' argument.")
|
||||
}
|
||||
|
||||
var result = paths.map(p => {
|
||||
return getFileIntegrity(p, algos)
|
||||
})
|
||||
console.log(result)
|
||||
if (!['json', 'text'].includes(output)) {
|
||||
throw new Error("Expected either 'json' or 'text' in the 'output' argument.")
|
||||
}
|
||||
|
||||
var result = {}
|
||||
for (const p of paths) {
|
||||
result[p] = await getFileIntegrity(p, algos)
|
||||
}
|
||||
|
||||
if (output == 'json') {
|
||||
return JSON.stringify(result)
|
||||
} else {
|
||||
var text_result = ''
|
||||
for (const p of paths) {
|
||||
text_result += `${p}: ${result[p].join(' ')}\n`
|
||||
}
|
||||
return text_result
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -124,6 +148,12 @@ const pluginActions = {
|
|||
collect: true,
|
||||
string: true,
|
||||
default: "SHA-256"
|
||||
},
|
||||
output: {
|
||||
description: "a string, defining output mode ('json' or 'text'; 'json' is default)",
|
||||
collect: false,
|
||||
string: true,
|
||||
default: "json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue