-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
46 lines (46 loc) · 1.54 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { parseArgs } from "jsr:@std/cli@^1.0.8/parse-args";
import { DJB2a } from "./mod.ts";
if (!import.meta.main) {
throw new Error(`This script is for command line usage only!`);
}
const args = parseArgs(Deno.args, {
boolean: [
"file",
"stdin"
]
});
const fromFile: boolean = args.file;
const fromStdin: boolean = args.stdin;
const argsValues: string[] = args._.map((value: string | number): string => {
return String(value);
});
if (fromFile && fromStdin) {
throw new SyntaxError(`Unable to use the sources of file and stdin together!`);
}
if (fromFile) {
if (argsValues.length === 0) {
throw new SyntaxError(`File path is not defined!`);
}
if (argsValues.length !== 1) {
throw new SyntaxError(`Too many arguments! Expect: 1; Current: ${argsValues.length}.`);
}
using file: Deno.FsFile = await Deno.open(argsValues[0]);
console.log((await DJB2a.fromStream(file.readable)).hashHexPadding());
} else if (fromStdin) {
if (argsValues.length !== 0) {
throw new SyntaxError(`Too many arguments! Expect: 0; Current: ${argsValues.length}.`);
}
let data: Uint8Array = Uint8Array.from([]);
for await (const chunk of Deno.stdin.readable) {
data = Uint8Array.from([...data, ...chunk]);
}
console.log(new DJB2a(new TextDecoder().decode(data).replace(/\r?\n$/, "")).hashHexPadding());
} else {
if (argsValues.length === 0) {
throw new SyntaxError(`Data is not defined!`);
}
if (argsValues.length !== 1) {
throw new SyntaxError(`Too many arguments! Expect: 1; Current: ${argsValues.length}.`);
}
console.log(new DJB2a(argsValues[0]).hashHexPadding());
}