diff --git a/src/main.ts b/src/main.ts index b63961f4..ec2c4c31 100644 --- a/src/main.ts +++ b/src/main.ts @@ -170,6 +170,14 @@ function assertFormat(value: string, allowed: readonly string[], command: string } } +type AliasRow = { from: string; to: string } + +function toAliasRows(aliases: Record): AliasRow[] { + return Object.entries(aliases) + .map(([from, to]) => ({ from, to })) + .sort((a, b) => a.from < b.from ? -1 : a.from > b.from ? 1 : 0) +} + function assertProvider(value: string, command: string): void { const names = allProviderNames() if (value === 'all' || names.includes(value)) return @@ -1030,11 +1038,18 @@ program .description('Map a provider model name to a canonical one for pricing (e.g. codeburn model-alias my-model claude-opus-4-6)') .option('--remove ', 'Remove an alias') .option('--list', 'List configured aliases') - .action(async (from?: string, to?: string, opts?: { remove?: string; list?: boolean }) => { + .option('--format ', 'Output format: text, json', 'text') + .action(async (from?: string, to?: string, opts?: { remove?: string; list?: boolean; format?: string }) => { + const format = opts?.format ?? 'text' + assertFormat(format, ['text', 'json'], 'model-alias') const config = await readConfig() const aliases = config.modelAliases ?? {} if (opts?.list || (!from && !opts?.remove)) { + if (format === 'json') { + console.log(JSON.stringify(toAliasRows(aliases), null, 2)) + return + } const entries = Object.entries(aliases) if (entries.length === 0) { console.log('\n No model aliases configured.') @@ -1218,7 +1233,10 @@ program .description('Mark a project directory as routed through a subscription-backed LLM proxy (e.g. Claude Code over GitHub Copilot). Sessions whose canonical path is under it keep their full API-rate cost as the "would-be" figure, but that amount is reported as subscription-covered so the report can show net out-of-pocket (e.g. codeburn proxy-path ~/work/copilot-repo). Actual API-key sessions elsewhere are untouched.') .option('--remove ', 'Remove a configured proxy path') .option('--list', 'List configured proxy paths') - .action(async (path?: string, opts?: { remove?: string; list?: boolean }) => { + .option('--format ', 'Output format: text, json', 'text') + .action(async (path?: string, opts?: { remove?: string; list?: boolean; format?: string }) => { + const format = opts?.format ?? 'text' + assertFormat(format, ['text', 'json'], 'proxy-path') const config = await readConfig() // Sanitize the on-disk shape the same way setProxyPaths does: a hand-edited // config.json could have proxyPaths as a non-array or hold non-string @@ -1228,6 +1246,10 @@ program const samePath = (a: string, b: string) => normalizeProxyPath(a) === normalizeProxyPath(b) if (opts?.list || (!path && !opts?.remove)) { + if (format === 'json') { + console.log(JSON.stringify(paths, null, 2)) + return + } if (paths.length === 0) { console.log('\n No proxy paths configured.') console.log(` Config: ${getConfigFilePath()}`) diff --git a/tests/cli-settings-json.test.ts b/tests/cli-settings-json.test.ts new file mode 100644 index 00000000..0d37cb67 --- /dev/null +++ b/tests/cli-settings-json.test.ts @@ -0,0 +1,58 @@ +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { spawnSync } from 'node:child_process' + +import { afterEach, describe, expect, it, vi } from 'vitest' + +vi.setConfig({ testTimeout: 30_000 }) + +const homes: string[] = [] + +afterEach(async () => { + while (homes.length > 0) { + const home = homes.pop() + if (home) await rm(home, { recursive: true, force: true }) + } +}) + +async function makeHome(): Promise { + const home = await mkdtemp(join(tmpdir(), 'codeburn-settings-json-')) + homes.push(home) + return home +} + +function runCli(args: string[], home: string) { + return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], { + cwd: process.cwd(), + env: { ...process.env, HOME: home, CLAUDE_CONFIG_DIR: join(home, '.claude'), TZ: 'UTC' }, + encoding: 'utf-8', + timeout: 30_000, + }) +} + +describe('settings CLI JSON list output', () => { + it('lists model aliases as rows sorted by from', async () => { + const home = await makeHome() + + expect(runCli(['model-alias', 'z-model', 'gpt-4o'], home).status).toBe(0) + expect(runCli(['model-alias', 'a-model', 'claude-sonnet-4-6'], home).status).toBe(0) + + const result = runCli(['model-alias', '--list', '--format', 'json'], home) + expect(result.status).toBe(0) + expect(JSON.parse(result.stdout)).toEqual([ + { from: 'a-model', to: 'claude-sonnet-4-6' }, + { from: 'z-model', to: 'gpt-4o' }, + ]) + }) + + it('lists configured proxy paths as strings', async () => { + const home = await makeHome() + + expect(runCli(['proxy-path', '/work/copilot-repo'], home).status).toBe(0) + + const result = runCli(['proxy-path', '--list', '--format', 'json'], home) + expect(result.status).toBe(0) + expect(JSON.parse(result.stdout)).toEqual(['/work/copilot-repo']) + }) +})