| 21 | const BASE_URL = `https://api.fly.io/victorialogs/${FLY_ORG_SLUG}/select/logsql/query` |
| 22 | |
| 23 | function parseArgs() { |
| 24 | const args = process.argv.slice(2) |
| 25 | const opts: Record<string, string> = {} |
| 26 | for (let i = 0; i < args.length; i++) { |
| 27 | if (args[i] === '--token' || args[i] === '-t') opts.token = args[++i] |
| 28 | else if (args[i] === '--app' || args[i] === '-a') opts.app = args[++i] |
| 29 | else if (args[i] === '--start') opts.start = args[++i] |
| 30 | else if (args[i] === '--end') opts.end = args[++i] |
| 31 | else if (args[i] === '--last' || args[i] === '-l') opts.last = args[++i] |
| 32 | else if (args[i] === '--filter' || args[i] === '-f') opts.filter = args[++i] |
| 33 | else if (args[i] === '--limit') opts.limit = args[++i] |
| 34 | else if (args[i] === '--output' || args[i] === '-o') opts.output = args[++i] |
| 35 | else if (args[i] === '--pretty' || args[i] === '-p') opts.pretty = 'true' |
| 36 | else if (args[i] === '--help' || args[i] === '-h') { |
| 37 | console.log( |
| 38 | [ |
| 39 | 'Usage: npx tsx internal/scripts/fetch-fly-logs.ts [options]', |
| 40 | '', |
| 41 | 'Options:', |
| 42 | ' --token, -t Fly API token (falls back to FLY_TOKEN env var)', |
| 43 | ' --app, -a Fly app name (required)', |
| 44 | ' --last, -l Duration to look back, e.g. 30m, 2h, 1d (default: 1h)', |
| 45 | ' --start Start time (ISO 8601), overrides --last', |
| 46 | ' --end End time (ISO 8601), defaults to now', |
| 47 | ' --filter, -f Additional LogsQL filter, e.g. "Purged", "NOT DEBUG"', |
| 48 | ' --limit Max lines (default: unlimited)', |
| 49 | ' --output, -o Write to file instead of stdout', |
| 50 | ' --pretty, -p Pretty-print: timestamp + message only', |
| 51 | ' --help, -h Show this help', |
| 52 | ].join('\n') |
| 53 | ) |
| 54 | process.exit(0) |
| 55 | } else { |
| 56 | console.error(`Unknown option: ${args[i]}`) |
| 57 | process.exit(1) |
| 58 | } |
| 59 | } |
| 60 | if (!opts.app) { |
| 61 | console.error('--app is required') |
| 62 | process.exit(1) |
| 63 | } |
| 64 | return opts |
| 65 | } |
| 66 | |
| 67 | async function main() { |
| 68 | const opts = parseArgs() |