From 5a91ec2da6a63bd31b1f26efaad01b7dc71cd20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81valos?= Date: Mon, 3 Jul 2023 13:27:44 -0600 Subject: Move anastasis CLI into separate package --- packages/anastasis-cli/Makefile | 41 ++++++++++++++++ packages/anastasis-cli/README.md | 4 ++ packages/anastasis-cli/bin/anastasis-cli.mjs | 20 ++++++++ packages/anastasis-cli/build-node.mjs | 70 +++++++++++++++++++++++++++ packages/anastasis-cli/package.json | 44 +++++++++++++++++ packages/anastasis-cli/src/import-meta-url.js | 2 + packages/anastasis-cli/src/index.ts | 64 ++++++++++++++++++++++++ packages/anastasis-cli/tsconfig.json | 33 +++++++++++++ 8 files changed, 278 insertions(+) create mode 100644 packages/anastasis-cli/Makefile create mode 100644 packages/anastasis-cli/README.md create mode 100755 packages/anastasis-cli/bin/anastasis-cli.mjs create mode 100755 packages/anastasis-cli/build-node.mjs create mode 100644 packages/anastasis-cli/package.json create mode 100644 packages/anastasis-cli/src/import-meta-url.js create mode 100644 packages/anastasis-cli/src/index.ts create mode 100644 packages/anastasis-cli/tsconfig.json (limited to 'packages/anastasis-cli') diff --git a/packages/anastasis-cli/Makefile b/packages/anastasis-cli/Makefile new file mode 100644 index 000000000..292f7000f --- /dev/null +++ b/packages/anastasis-cli/Makefile @@ -0,0 +1,41 @@ +# This Makefile has been placed in the public domain. + +ifeq ($(TOPLEVEL), yes) + $(info top-level build) + -include ../../.config.mk +else + $(info package-level build) + -include ../../.config.mk + -include .config.mk +endif + +$(info prefix is $(prefix)) + +all: + @echo use 'make install' to build and install anastasis-cli + +ifndef prefix +.PHONY: warn-noprefix install +warn-noprefix: + @echo "no prefix configured, did you run ./configure?" +install: warn-noprefix +else +install_target = $(prefix)/lib/anastasis-cli +.PHONY: install install-nodeps deps +install-nodeps: + ./build-node.mjs + install -d $(prefix)/bin + install -d $(install_target)/bin + install -d $(install_target)/node_modules/anastasis-cli + install -d $(install_target)/node_modules/anastasis-cli/bin + install -d $(install_target)/node_modules/anastasis-cli/dist + install ./dist/anastasis-cli-bundled.cjs $(install_target)/node_modules/anastasis-cli/dist/ + install ./dist/anastasis-cli-bundled.cjs.map $(install_target)/node_modules/anastasis-cli/dist/ + install ./bin/anastasis-cli.mjs $(install_target)/node_modules/anastasis-cli/bin/ + ln -sf $(install_target)/node_modules/anastasis-cli/bin/anastasis-cli.mjs $(prefix)/bin/anastasis-cli +deps: + pnpm install --frozen-lockfile --filter @gnu-taler/anastasis-cli... +install: + $(MAKE) deps + $(MAKE) install-nodeps +endif diff --git a/packages/anastasis-cli/README.md b/packages/anastasis-cli/README.md new file mode 100644 index 000000000..a48fd3c51 --- /dev/null +++ b/packages/anastasis-cli/README.md @@ -0,0 +1,4 @@ +# anastasis-cli + +This package provides `anastasis-cli`, the command-line interface for the +Anastasis backup system. diff --git a/packages/anastasis-cli/bin/anastasis-cli.mjs b/packages/anastasis-cli/bin/anastasis-cli.mjs new file mode 100755 index 000000000..7506e4ba7 --- /dev/null +++ b/packages/anastasis-cli/bin/anastasis-cli.mjs @@ -0,0 +1,20 @@ +#!/usr/bin/env node +/* + This file is part of GNU Taler + (C) 2022 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +import { reducerCliMain } from '../dist/anastasis-cli-bundled.cjs'; + +reducerCliMain(); diff --git a/packages/anastasis-cli/build-node.mjs b/packages/anastasis-cli/build-node.mjs new file mode 100755 index 000000000..04b1c5256 --- /dev/null +++ b/packages/anastasis-cli/build-node.mjs @@ -0,0 +1,70 @@ +#!/usr/bin/env node +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +import esbuild from "esbuild"; +import path from "path"; +import fs from "fs"; + +const BASE = process.cwd(); + +let GIT_ROOT = BASE; +while (!fs.existsSync(path.join(GIT_ROOT, ".git")) && GIT_ROOT !== "/") { + GIT_ROOT = path.join(GIT_ROOT, "../"); +} +if (GIT_ROOT === "/") { + console.log("not found"); + process.exit(1); +} +const GIT_HASH = GIT_ROOT === "/" ? undefined : git_hash(); + +let _package = JSON.parse(fs.readFileSync(path.join(BASE, "package.json"))); + +function git_hash() { + const rev = fs + .readFileSync(path.join(GIT_ROOT, ".git", "HEAD")) + .toString() + .trim() + .split(/.*[: ]/) + .slice(-1)[0]; + if (rev.indexOf("/") === -1) { + return rev; + } else { + return fs.readFileSync(path.join(GIT_ROOT, ".git", rev)).toString().trim(); + } +} + +export const buildConfig = { + entryPoints: ["src/index.ts"], + outfile: "dist/anastasis-cli-bundled.cjs", + bundle: true, + minify: false, + target: ["es2020"], + format: "cjs", + platform: "node", + sourcemap: true, + inject: ["src/import-meta-url.js"], + define: { + __VERSION__: `"${_package.version}"`, + __GIT_HASH__: `"${GIT_HASH}"`, + ["import.meta.url"]: "import_meta_url", + }, +}; + +esbuild.build(buildConfig).catch((e) => { + console.log(e); + process.exit(1); +}); diff --git a/packages/anastasis-cli/package.json b/packages/anastasis-cli/package.json new file mode 100644 index 000000000..1821cd696 --- /dev/null +++ b/packages/anastasis-cli/package.json @@ -0,0 +1,44 @@ +{ + "name": "@gnu-taler/anastasis-cli", + "version": "0.0.1", + "description": "", + "engines": { + "node": ">=0.18.0" + }, + "repository": { + "type": "git", + "url": "git://git.taler.net/wallet-core.git" + }, + "author": "Florian Dold", + "license": "GPL-3.0", + "bin": { + "anastasis-cli": "./bin/anastasis-cli.mjs" + }, + "type": "module", + "scripts": { + "compile": "tsc --build && ./build-node.mjs", + "test": "tsc", + "clean": "rimraf lib dist tsconfig.tsbuildinfo", + "pretty": "prettier --write src" + }, + "files": [ + "AUTHORS", + "README", + "COPYING", + "bin/", + "dist/node", + "src/" + ], + "devDependencies": { + "@types/node": "^18.11.17", + "prettier": "^2.8.8", + "rimraf": "^3.0.2", + "typedoc": "^0.24.8", + "typescript": "^5.1.3" + }, + "dependencies": { + "@gnu-taler/taler-util": "workspace:*", + "@gnu-taler/anastasis-core": "workspace:*", + "tslib": "^2.5.3" + } +} 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 { + 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(); +} diff --git a/packages/anastasis-cli/tsconfig.json b/packages/anastasis-cli/tsconfig.json new file mode 100644 index 000000000..83da34277 --- /dev/null +++ b/packages/anastasis-cli/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "composite": true, + "target": "ES2018", + "module": "ESNext", + "moduleResolution": "Node16", + "sourceMap": true, + "lib": ["es6"], + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "strict": true, + "strictPropertyInitialization": false, + "outDir": "lib", + "noImplicitAny": true, + "noImplicitThis": true, + "incremental": true, + "esModuleInterop": true, + "importHelpers": true, + "rootDir": "src", + "baseUrl": "./src", + "typeRoots": ["./node_modules/@types"] + }, + "include": ["src/**/*"], + "references": [ + { + "path": "../anastasis-core/" + }, + { + "path": "../taler-util/" + } + ] +} -- cgit v1.2.3