import type { Command } from "commander"; import { createClient } from "../../api/client.ts"; import { captureTokenViaBrowser, SETUP_ALLOWED_ORIGIN, } from "../../auth/browser-flow.ts"; import { type AuthScope, authLoginUrl, configPath, getAppApiKey, loadConfig, removeProfile, setAppApiKey, setDefaultProfile, upsertProfile, } from "../../auth/profiles.ts"; import { openInBrowser, readLine } from "../../util/runtime.ts"; import { failure, printResult, success } from "../context.ts"; import { rootOpts } from "./run.ts"; const POWER_UPS_ADMIN = "https://trello.com/power-ups/admin"; export function registerAuthCommands(program: Command): void { const auth = program.command("auth").description("Manage Trello credentials"); auth .command("setup") .description("One-time app setup: save API key + allowed-origin instructions") .option("-k, --api-key ", "Trello API key to save") .option("--no-open", "Do not open Power-Ups admin in the browser") .action(async (opts, cmd) => { const root = rootOpts(cmd); try { if (opts.open !== false) { process.stderr.write(`Opening ${POWER_UPS_ADMIN}\n`); await openInBrowser(POWER_UPS_ADMIN).catch(() => { process.stderr.write(`Open manually: ${POWER_UPS_ADMIN}\n`); }); } process.stderr.write(` One-time setup (per Trello account / Power-Up): 1. Create or open a Power-Up → API Key tab → Generate API Key (Pick any workspace you admin — personal is fine. This does not limit which boards you can use. Never install the app on a board.) 2. Allowed Origins → add: ${SETUP_ALLOWED_ORIGIN} (required for automatic browser login) 3. Paste your API key below `); let apiKey = opts.apiKey as string | undefined; if (!apiKey) { process.stderr.write("API key: "); apiKey = (await readLine()).trim(); } if (!apiKey) { throw new Error("API key is required"); } setAppApiKey(apiKey); printResult( success("default", { message: "Saved app API key for trelly", configPath: configPath(), allowedOrigin: SETUP_ALLOWED_ORIGIN, next: "Run: trelly auth login", }), root, ); } catch (err) { printResult(failure(err instanceof Error ? err.message : String(err)), root); process.exitCode = 1; } }); auth .command("login") .description("Authorize a profile via browser (default) or manual token paste") .option("-p, --profile ", "Profile name", "default") .option("-k, --api-key ", "Override app API key for this login") .option("-t, --token ", "Token (skips browser flow)") .option( "--manual", "Skip browser callback; paste token after opening authorize URL", ) .option("--no-open", "Do not open a browser automatically") .option("--default", "Set as default profile") .option("--label