|
| 1 | +import yargs from 'yargs'; |
| 2 | +import { hideBin } from 'yargs/helpers'; |
| 3 | + |
| 4 | +const NETWORK_CHOICES = ['tcp', 'tcp4', 'tcp6', 'unix', 'unixpacket'] as const; |
| 5 | +const LOG_LEVEL_CHOICES = ['trace', 'debug', 'info', 'warn', 'error'] as const; |
| 6 | +const LOG_FORMAT_CHOICES = ['json', 'text'] as const; |
| 7 | +const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const; |
| 8 | + |
| 9 | +export type ServeArgs = { |
| 10 | + address: string; |
| 11 | + network: (typeof NETWORK_CHOICES)[number]; |
| 12 | + logLevel: (typeof LOG_LEVEL_CHOICES)[number]; |
| 13 | + logFormat: (typeof LOG_FORMAT_CHOICES)[number]; |
| 14 | + sentry: boolean; |
| 15 | + otelEndpoint: string; |
| 16 | + otelEndpointInsecure: boolean; |
| 17 | + telemetryLevel: (typeof TELEMETRY_LEVEL_CHOICES)[number]; |
| 18 | +}; |
| 19 | + |
| 20 | +export const serve = yargs(hideBin(process.argv)) |
| 21 | + .command<ServeArgs>( |
| 22 | + 'serve', |
| 23 | + 'start plugin gRPC server', |
| 24 | + () => {}, |
| 25 | + ({ address, network, logLevel, logFormat, sentry: sentry, otelEndpoint, telemetryLevel }: ServeArgs) => { |
| 26 | + console.log({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel }); |
| 27 | + }, |
| 28 | + ) |
| 29 | + .options({ |
| 30 | + address: { |
| 31 | + alias: 'a', |
| 32 | + type: 'string', |
| 33 | + description: 'address to bind to', |
| 34 | + default: 'localhost:7777', |
| 35 | + }, |
| 36 | + network: { |
| 37 | + alias: 'n', |
| 38 | + type: 'string', |
| 39 | + choices: NETWORK_CHOICES, |
| 40 | + description: 'network to bind to', |
| 41 | + default: 'tcp', |
| 42 | + }, |
| 43 | + 'log-level': { |
| 44 | + alias: 'l', |
| 45 | + type: 'string', |
| 46 | + choices: LOG_LEVEL_CHOICES, |
| 47 | + description: 'log level', |
| 48 | + default: 'info', |
| 49 | + }, |
| 50 | + 'log-format': { |
| 51 | + alias: 'f', |
| 52 | + type: 'string', |
| 53 | + choices: LOG_FORMAT_CHOICES, |
| 54 | + description: 'log format', |
| 55 | + default: 'text', |
| 56 | + }, |
| 57 | + sentry: { |
| 58 | + type: 'boolean', |
| 59 | + description: 'enable sentry reporting. Pass `--no-sentry` to disable.', |
| 60 | + default: true, |
| 61 | + }, |
| 62 | + 'otel-endpoint': { |
| 63 | + type: 'string', |
| 64 | + description: 'OpenTelemetry collector endpoint', |
| 65 | + default: '', |
| 66 | + }, |
| 67 | + 'otel-endpoint-insecure': { |
| 68 | + type: 'boolean', |
| 69 | + description: 'use Open Telemetry HTTP endpoint (for development only)', |
| 70 | + default: false, |
| 71 | + }, |
| 72 | + 'telemetry-level': { |
| 73 | + type: 'string', |
| 74 | + description: 'CQ Telemetry level', |
| 75 | + hidden: true, |
| 76 | + choices: TELEMETRY_LEVEL_CHOICES, |
| 77 | + default: 'all', |
| 78 | + }, |
| 79 | + }) |
| 80 | + .env('CQ_') |
| 81 | + .strict() |
| 82 | + .demandCommand(1, 1, 'Specify a command to run'); |
0 commit comments