|
| 1 | +import { ExitCode } from '@stacksjs/types' |
| 2 | +import type { CLI } from '@stacksjs/types' |
| 3 | +import { config } from '@stacksjs/config' |
| 4 | +import { runCommand } from '@stacksjs/cli' |
| 5 | + |
| 6 | +// import { Action } from '@stacksjs/enums' |
| 7 | +// import { runAction } from '@stacksjs/actions' |
| 8 | + |
| 9 | +type DnsOptions = { |
| 10 | + query?: string |
| 11 | + type?: string |
| 12 | + nameserver?: string |
| 13 | + class?: string |
| 14 | + udp?: boolean |
| 15 | + tcp?: boolean |
| 16 | + tls?: boolean |
| 17 | + https?: boolean |
| 18 | + short?: boolean |
| 19 | + json?: boolean |
| 20 | + pretty?: boolean |
| 21 | + p?: boolean // short for pretty |
| 22 | + verbose?: boolean |
| 23 | +} |
| 24 | + |
| 25 | +export function dns(buddy: CLI) { |
| 26 | + const descriptions = { |
| 27 | + dns: 'Lists the DNS records for a domain', |
| 28 | + query: 'Host name or IP address to query', |
| 29 | + type: 'Type of the DNS record being queried (A, MX, NS…)', |
| 30 | + nameserver: 'Address of the nameserver to send packets to', |
| 31 | + class: 'Network class of the DNS record being queried (IN, CH, HS)', |
| 32 | + udp: 'Use the DNS protocol over UDP', |
| 33 | + tcp: 'Use the DNS protocol over TCP', |
| 34 | + tls: 'Use the DNS-over-TLS protocol', |
| 35 | + https: 'Use the DNS-over-HTTPS protocol', |
| 36 | + short: 'Short mode: display nothing but the first result', |
| 37 | + json: 'Display the output as JSON', |
| 38 | + pretty: 'Display the output as JSON in a pretty format', |
| 39 | + verbose: 'Enable verbose output', |
| 40 | + } |
| 41 | + |
| 42 | + buddy |
| 43 | + .command('dns [domain]', descriptions.dns) |
| 44 | + .option('-q, --query <query>', descriptions.query) |
| 45 | + .option('-t, --type <type>', descriptions.type, { default: 'ANY' }) |
| 46 | + .option('-n, --nameserver <nameserver>', descriptions.nameserver) |
| 47 | + .option('--class <class>', descriptions.nameserver) |
| 48 | + // transport options |
| 49 | + .option('-U, --udp', descriptions.udp) |
| 50 | + .option('-T, --tcp', descriptions.tcp) |
| 51 | + .option('-S, --tls', descriptions.tls) |
| 52 | + .option('-H, --https', descriptions.https) |
| 53 | + // output options |
| 54 | + .option('-1, --short', descriptions.short, { default: false }) |
| 55 | + .option('-J, --json', descriptions.json, { default: false }) |
| 56 | + .option('-p, --pretty', descriptions.pretty, { default: true }) |
| 57 | + .option('--verbose', descriptions.verbose, { default: false }) |
| 58 | + .action(async (domain: string | undefined, options: DnsOptions) => { |
| 59 | + let prettyOutput = false |
| 60 | + |
| 61 | + if (options.json && options.pretty) |
| 62 | + prettyOutput = true |
| 63 | + |
| 64 | + delete options.pretty |
| 65 | + delete options.p |
| 66 | + |
| 67 | + // Convert options object to command-line options string |
| 68 | + const optionsString = Object.entries(options) |
| 69 | + .filter(([key, value]) => key !== '--' && key.length > 1 && value !== false) // filter out '--' key and short options |
| 70 | + .map(([key, value]) => `--${key} ${value}`) |
| 71 | + .join(' ') |
| 72 | + |
| 73 | + await runCommand(`dog ${domain || config.app.url } ${optionsString}`) |
| 74 | + |
| 75 | + process.exit(ExitCode.Success) |
| 76 | + }) |
| 77 | +} |
0 commit comments