import chalk from 'chalk'; import type { CommandModule } from 'yargs'; import type { ServiceLogsOptions } from './service-logs-options.js'; import { getServiceLogs, validateServiceLogsArgs } from './service-logs.js'; const { red, yellow } = chalk; export const serviceLogs: CommandModule = { builder: (yargs) => { return yargs .option('serviceId', { describe: 'Service ID to read runtime logs for', alias: 'i', string: true, demandOption: true, }) .option('tail', { describe: 'Maximum number of log lines to show per replica', alias: 't', number: true, default: 100, }) .option('since', { describe: 'Only show log lines newer than the given age, for example 30s, 5m, 2h or 1d. A plain number is read as seconds.', string: true, }) .option('timestamps', { describe: 'Show the timestamp Kubernetes recorded for each line. Lines are ordered by it either way.', boolean: true, }) .option('json', { describe: 'Emit one JSON object per line ({replicaName, timestamp, message}), in the same order as the normal output. Makes --prefix and --timestamps redundant.', boolean: true, }) .option('prefix', { describe: 'Prefix each line with the replica it came from. On by default when the service has more than one replica. Use --no-prefix when piping the raw lines to tools such as grep or awk.', boolean: true, }) .option('mosaicHostingClientId', { describe: 'Service Account Client ID to authenticate', alias: 'c', string: true, }) .option('mosaicHostingClientSecret', { describe: 'Service Account Client Secret to authenticate', alias: 's', string: true, }) .option('idServiceAuthBaseURL', { describe: 'ID Service Authentication Endpoint Base URL', alias: 'a', string: true, }) .option('hostingServiceBaseURL', { describe: 'Hosting Service Base URL', alias: 'h', string: true, }) .example( '$0 hosting service logs -i channel-service', 'Show the last 100 log lines of each replica', ) .example( '$0 hosting service logs -i channel-service --tail 500 > service.log', 'Write log lines to a file (status messages stay on stderr)', ); }, handler: async (args) => { const [validatedArgs, errorMessages] = validateServiceLogsArgs(args); if (errorMessages.length > 0) { process.stderr.write(red('Some required arguments are missing.\n')); process.stderr.write(yellow(errorMessages.join('\n') + '\n')); process.exit(1); } else { await getServiceLogs( validatedArgs as ServiceLogsOptions & { serviceId: string }, ); } }, };