Something went wrong. Try again.
Something went wrong. Try again.
2.7 kB · 75 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#!/usr/bin/env bunimport { Command } from "commander";import { authCommand } from "./commands/auth.ts";import { repoCommand } from "./commands/repo.ts";import { cloneCommand } from "./commands/clone.ts";import { collabCommand } from "./commands/collab.ts";import { pipelineCommand } from "./commands/pipeline.ts";import { lexiconCommand } from "./commands/lexicon.ts";import { whoamiCommand, resolveCommand } from "./commands/identity.ts";import { recordCommand } from "./commands/record.ts";import { xrpcCommand } from "./commands/xrpc.ts";import { refCommand, fileCommand } from "./commands/ref.ts";import { secretCommand } from "./commands/secret.ts";import { batchCommand } from "./commands/batch.ts";import { XrpcError } from "./pds.ts";import { EXIT, TangleError } from "./errors.ts";
const program = new Command();program .name("tangle") .description("CLI for tangled — atproto-native git collaboration") .version("0.1.0") .option("--json", "emit machine-readable JSON output (command-specific shape)");
program.addCommand(authCommand());program.addCommand(repoCommand());program.addCommand(cloneCommand());program.addCommand(collabCommand());program.addCommand(pipelineCommand());program.addCommand(lexiconCommand());program.addCommand(whoamiCommand());program.addCommand(resolveCommand());program.addCommand(recordCommand());program.addCommand(xrpcCommand());program.addCommand(refCommand());program.addCommand(fileCommand());program.addCommand(secretCommand());program.addCommand(batchCommand());
try { await program.parseAsync(process.argv);} catch (e) { emitError(e); process.exit(exitCodeFor(e));}
function exitCodeFor(e: unknown): number { if (e instanceof TangleError) return e.code; if (e instanceof XrpcError) { if (e.status === 401 || e.status === 403) return EXIT.AUTH; if (e.status === 404) return EXIT.NOT_FOUND; if (e.status === 409) return EXIT.CONFLICT; return EXIT.GENERIC; } return EXIT.GENERIC;}
function emitError(e: unknown): void { const globalJson = (program.opts() as { json?: boolean }).json === true; const { name, message } = errorShape(e); if (globalJson) { process.stderr.write(JSON.stringify({ error: name, message }) + "\n"); } else { process.stderr.write(`error: ${message}\n`); }}
function errorShape(e: unknown): { name: string; message: string } { if (e instanceof TangleError) return { name: e.errorName, message: e.message }; if (e instanceof XrpcError) { return { name: e.errorName ?? "XrpcError", message: e.message }; } if (e instanceof Error) return { name: e.name, message: e.message }; return { name: "Error", message: String(e) };}