| 12 | } from '../utils/format'; |
| 13 | |
| 14 | export function registerConfigCommand(program: Command) { |
| 15 | // ── whoami ──────────────────────────────────────────── |
| 16 | |
| 17 | program |
| 18 | .command('whoami') |
| 19 | .description('Display current user information') |
| 20 | .option('--json [fields]', 'Output JSON, optionally specify fields (comma-separated)') |
| 21 | .action(async (options: { json?: string | boolean }) => { |
| 22 | const client = await getTrpcClient(); |
| 23 | const state = await client.user.getUserState.query(); |
| 24 | |
| 25 | if (options.json !== undefined) { |
| 26 | const fields = typeof options.json === 'string' ? options.json : undefined; |
| 27 | outputJson(state, fields); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const s = state as any; |
| 32 | console.log(pc.bold('User Info')); |
| 33 | if (s.fullName || s.firstName) console.log(` Name: ${s.fullName || s.firstName}`); |
| 34 | if (s.username) console.log(` Username: ${s.username}`); |
| 35 | if (s.email) console.log(` Email: ${s.email}`); |
| 36 | if (s.userId) console.log(` User ID: ${s.userId}`); |
| 37 | if (s.subscriptionPlan) console.log(` Plan: ${s.subscriptionPlan}`); |
| 38 | }); |
| 39 | |
| 40 | // ── usage ───────────────────────────────────────────── |
| 41 | |
| 42 | program |
| 43 | .command('usage') |
| 44 | .description('View usage statistics') |
| 45 | .option('--month <YYYY-MM>', 'Month to query (default: current)') |
| 46 | .option('--daily', 'Group by day') |
| 47 | .option('--json [fields]', 'Output JSON, optionally specify fields (comma-separated)') |
| 48 | .action(async (options: { daily?: boolean; json?: string | boolean; month?: string }) => { |
| 49 | const client = await getTrpcClient(); |
| 50 | |
| 51 | const input: { mo?: string } = {}; |
| 52 | if (options.month) input.mo = options.month; |
| 53 | |
| 54 | if (options.json !== undefined) { |
| 55 | let jsonResult: any; |
| 56 | if (options.daily) { |
| 57 | jsonResult = await client.usage.findAndGroupByDay.query(input); |
| 58 | } else { |
| 59 | jsonResult = await client.usage.findByMonth.query(input); |
| 60 | } |
| 61 | const fields = typeof options.json === 'string' ? options.json : undefined; |
| 62 | outputJson(jsonResult, fields); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Always fetch daily-grouped data for table display |
| 67 | const result: any = await client.usage.findAndGroupByDay.query(input); |
| 68 | |
| 69 | if (!result) { |
| 70 | console.log('No usage data available.'); |
| 71 | return; |