|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { randomUUID } from 'node:crypto'; |
| 4 | +import { encodeCommandEnvelope } from '../src/command-envelope.js'; |
| 5 | + |
| 6 | +const usage = `alfredctl - emit Alfred Live control-plane commands (JSONL) |
| 7 | +
|
| 8 | +Usage: |
| 9 | + alfredctl list [prefix] [--id <id>] [--auth <token>] |
| 10 | + alfredctl read <path> [--id <id>] [--auth <token>] |
| 11 | + alfredctl write <path> <value> [--id <id>] [--auth <token>] |
| 12 | +
|
| 13 | +Options: |
| 14 | + --id <id> Command id (default: random UUID) |
| 15 | + --auth <token> Optional auth token |
| 16 | + -h, --help Show this help |
| 17 | +`; |
| 18 | + |
| 19 | +function fail(message) { |
| 20 | + process.stderr.write(`${message}\n\n${usage.trim()}\n`); |
| 21 | + process.exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +function redactSensitive(value) { |
| 25 | + if (Array.isArray(value)) { |
| 26 | + return value.map((entry) => redactSensitive(entry)); |
| 27 | + } |
| 28 | + if (!value || typeof value !== 'object') { |
| 29 | + return value; |
| 30 | + } |
| 31 | + |
| 32 | + const redacted = {}; |
| 33 | + for (const [key, entry] of Object.entries(value)) { |
| 34 | + if (/auth|token|password/i.test(key)) { |
| 35 | + redacted[key] = '[REDACTED]'; |
| 36 | + } else { |
| 37 | + redacted[key] = redactSensitive(entry); |
| 38 | + } |
| 39 | + } |
| 40 | + return redacted; |
| 41 | +} |
| 42 | + |
| 43 | +function parseArgs(argv) { |
| 44 | + const options = { id: undefined, auth: undefined }; |
| 45 | + const positionals = []; |
| 46 | + |
| 47 | + for (let i = 0; i < argv.length; i += 1) { |
| 48 | + const arg = argv[i]; |
| 49 | + |
| 50 | + if (arg === '--') { |
| 51 | + positionals.push(...argv.slice(i + 1)); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + if (arg === '-h' || arg === '--help') { |
| 56 | + return { help: true, options, positionals }; |
| 57 | + } |
| 58 | + |
| 59 | + if (arg === '--id' || arg === '--auth') { |
| 60 | + const value = argv[i + 1]; |
| 61 | + if (!value) { |
| 62 | + fail(`Missing value for ${arg}.`); |
| 63 | + } |
| 64 | + if (arg === '--id') { |
| 65 | + options.id = value; |
| 66 | + } else { |
| 67 | + options.auth = value; |
| 68 | + } |
| 69 | + i += 1; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (arg.startsWith('--')) { |
| 74 | + fail(`Unknown option: ${arg}`); |
| 75 | + } |
| 76 | + |
| 77 | + positionals.push(arg); |
| 78 | + } |
| 79 | + |
| 80 | + return { help: false, options, positionals }; |
| 81 | +} |
| 82 | + |
| 83 | +function buildEnvelope(positionals, options) { |
| 84 | + if (positionals.length === 0) { |
| 85 | + fail('Missing command.'); |
| 86 | + } |
| 87 | + |
| 88 | + const [command, ...rest] = positionals; |
| 89 | + const id = options.id ?? randomUUID(); |
| 90 | + const auth = options.auth; |
| 91 | + |
| 92 | + switch (command) { |
| 93 | + case 'list': { |
| 94 | + if (rest.length > 1) { |
| 95 | + fail('list accepts at most one prefix argument.'); |
| 96 | + } |
| 97 | + const args = rest[0] ? { prefix: rest[0] } : {}; |
| 98 | + return { id, cmd: 'list_config', args, auth }; |
| 99 | + } |
| 100 | + case 'read': { |
| 101 | + if (rest.length !== 1) { |
| 102 | + fail('read requires exactly one path argument.'); |
| 103 | + } |
| 104 | + return { id, cmd: 'read_config', args: { path: rest[0] }, auth }; |
| 105 | + } |
| 106 | + case 'write': { |
| 107 | + if (rest.length !== 2) { |
| 108 | + fail('write requires a path and value argument.'); |
| 109 | + } |
| 110 | + return { id, cmd: 'write_config', args: { path: rest[0], value: rest[1] }, auth }; |
| 111 | + } |
| 112 | + default: |
| 113 | + fail(`Unknown command: ${command}`); |
| 114 | + return null; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +const parsed = parseArgs(process.argv.slice(2)); |
| 119 | +if (parsed.help) { |
| 120 | + process.stdout.write(`${usage.trim()}\n`); |
| 121 | + process.exit(0); |
| 122 | +} |
| 123 | + |
| 124 | +const envelope = buildEnvelope(parsed.positionals, parsed.options); |
| 125 | +if (!envelope) { |
| 126 | + process.exit(1); |
| 127 | +} |
| 128 | +const encoded = encodeCommandEnvelope(envelope); |
| 129 | +if (!encoded.ok) { |
| 130 | + process.stderr.write(`${encoded.error.code}: ${encoded.error.message}\n`); |
| 131 | + if (encoded.error.details) { |
| 132 | + const redacted = redactSensitive(encoded.error.details); |
| 133 | + process.stderr.write(`${JSON.stringify(redacted, null, 2)}\n`); |
| 134 | + } |
| 135 | + process.exit(1); |
| 136 | +} |
| 137 | + |
| 138 | +process.stdout.write(`${encoded.data}\n`); |
0 commit comments