forked from mtusi2026/mtusi2026lab7
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { type GitTrackingSource } from "./git-tracking-proof";
|
|
export function parseGitProofCLI(args: string[]): GitTrackingSource {
|
|
const paths = args.filter((s) => s === "--git-proof").length,
|
|
hashes = args.filter((s) => s === "--git-proof-sha").length;
|
|
if (paths === 0 && hashes === 0) return { kind: "native-git" };
|
|
const path = args[args.indexOf("--git-proof") + 1],
|
|
sha256 = args[args.indexOf("--git-proof-sha") + 1];
|
|
if (
|
|
paths !== 1 ||
|
|
hashes !== 1 ||
|
|
typeof path !== "string" ||
|
|
path.length === 0 ||
|
|
path.startsWith("--") ||
|
|
/[\r\n\0]/.test(path) ||
|
|
!sha256 ||
|
|
!/^[a-f0-9]{64}$/.test(sha256)
|
|
)
|
|
throw Error("GIT_PROOF_CLI_INVALID");
|
|
return { kind: "canonical-proof", path, sha256 };
|
|
}
|
|
export function createAcceptCommand(ports: {
|
|
verifyProof: (
|
|
proof: Extract<GitTrackingSource, { kind: "canonical-proof" }>,
|
|
) => Promise<unknown>;
|
|
password: () => Promise<string>;
|
|
accept: (password: string, proof: GitTrackingSource) => Promise<void>;
|
|
}) {
|
|
if (
|
|
[ports?.verifyProof, ports?.password, ports?.accept].some(
|
|
(p) => typeof p !== "function",
|
|
)
|
|
)
|
|
throw Error("ACCEPT_COMMAND_PORTS_REQUIRED");
|
|
return async (args: string[]) => {
|
|
const proof = parseGitProofCLI(args);
|
|
if (proof.kind === "canonical-proof") await ports.verifyProof(proof);
|
|
await ports.accept(await ports.password(), proof);
|
|
};
|
|
}
|