diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-07-03 13:27:44 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-07-26 12:09:17 -0600 |
commit | 5a91ec2da6a63bd31b1f26efaad01b7dc71cd20b (patch) | |
tree | 898d03717ce3bc6be0270c6741ef07472a32cc49 /packages/anastasis-cli/src | |
parent | 72a0da7bbed253d86b24a49e1a1e8635b205de68 (diff) | |
download | wallet-core-5a91ec2da6a63bd31b1f26efaad01b7dc71cd20b.tar.xz |
Move anastasis CLI into separate package
Diffstat (limited to 'packages/anastasis-cli/src')
-rw-r--r-- | packages/anastasis-cli/src/import-meta-url.js | 2 | ||||
-rw-r--r-- | packages/anastasis-cli/src/index.ts | 64 |
2 files changed, 66 insertions, 0 deletions
diff --git a/packages/anastasis-cli/src/import-meta-url.js b/packages/anastasis-cli/src/import-meta-url.js new file mode 100644 index 000000000..c0e657160 --- /dev/null +++ b/packages/anastasis-cli/src/import-meta-url.js @@ -0,0 +1,2 @@ +// Helper to make 'import.meta.url' available in esbuild-bundled code as well. +export const import_meta_url = require("url").pathToFileURL(__filename); diff --git a/packages/anastasis-cli/src/index.ts b/packages/anastasis-cli/src/index.ts new file mode 100644 index 000000000..560574276 --- /dev/null +++ b/packages/anastasis-cli/src/index.ts @@ -0,0 +1,64 @@ +import { clk } from "@gnu-taler/taler-util/clk"; +import { + getBackupStartState, + getRecoveryStartState, + reduceAction, +} from "@gnu-taler/anastasis-core"; +import fs from "fs"; + +export const reducerCli = clk + .program("reducer", { + help: "Command line interface for Anastasis.", + }) + .flag("initBackup", ["-b", "--backup"]) + .flag("initRecovery", ["-r", "--restore"]) + .maybeOption("argumentsJson", ["-a", "--arguments"], clk.STRING) + .maybeArgument("action", clk.STRING) + .maybeArgument("stateFile", clk.STRING); + +async function read(stream: NodeJS.ReadStream): Promise<string> { + const chunks = []; + for await (const chunk of stream) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +reducerCli.action(async (x) => { + if (x.reducer.initBackup) { + console.log(JSON.stringify(await getBackupStartState())); + return; + } else if (x.reducer.initRecovery) { + console.log(JSON.stringify(await getRecoveryStartState())); + return; + } + + const action = x.reducer.action; + if (!action) { + console.log("action required"); + return; + } + + let lastState: any; + if (x.reducer.stateFile) { + const s = fs.readFileSync(x.reducer.stateFile, { encoding: "utf-8" }); + lastState = JSON.parse(s); + } else { + const s = await read(process.stdin); + lastState = JSON.parse(s); + } + + let args: any; + if (x.reducer.argumentsJson) { + args = JSON.parse(x.reducer.argumentsJson); + } else { + args = {}; + } + + const nextState = await reduceAction(lastState, action, args); + console.log(JSON.stringify(nextState)); +}); + +export function reducerCliMain() { + reducerCli.run(); +} |