()
| 504 | * Returns SSH config or null if no SSH options are provided |
| 505 | */ |
| 506 | export function resolveSSHConfig(): { config: SSHTunnelConfig; source: string } | null { |
| 507 | // Get command line arguments |
| 508 | const args = parseCommandLineArgs(); |
| 509 | |
| 510 | // Check if any SSH options are provided |
| 511 | const hasSSHArgs = args["ssh-host"] || process.env.SSH_HOST; |
| 512 | if (!hasSSHArgs) { |
| 513 | return null; |
| 514 | } |
| 515 | |
| 516 | // Build SSH config from command line and environment variables |
| 517 | let config: Partial<SSHTunnelConfig> = {}; |
| 518 | let sources: string[] = []; |
| 519 | let sshConfigHost: string | undefined; |
| 520 | |
| 521 | // SSH Host (required) |
| 522 | if (args["ssh-host"]) { |
| 523 | sshConfigHost = args["ssh-host"]; |
| 524 | config.host = args["ssh-host"]; |
| 525 | sources.push("ssh-host from command line"); |
| 526 | } else if (process.env.SSH_HOST) { |
| 527 | sshConfigHost = process.env.SSH_HOST; |
| 528 | config.host = process.env.SSH_HOST; |
| 529 | sources.push("SSH_HOST from environment"); |
| 530 | } |
| 531 | |
| 532 | // Check if the host looks like an SSH config alias |
| 533 | if (sshConfigHost && looksLikeSSHAlias(sshConfigHost)) { |
| 534 | // Try to parse SSH config for this host, default to ~/.ssh/config |
| 535 | const sshConfigPath = getDefaultSSHConfigPath(); |
| 536 | console.error(`Attempting to parse SSH config for host '${sshConfigHost}' from: ${sshConfigPath}`); |
| 537 | const sshConfigData = parseSSHConfig(sshConfigHost, sshConfigPath); |
| 538 | if (sshConfigData) { |
| 539 | // Use SSH config as base, but allow command line/env to override |
| 540 | config = { ...sshConfigData }; |
| 541 | sources.push(`SSH config for host '${sshConfigHost}'`); |
| 542 | |
| 543 | // The host from SSH config has already been set, no need to override |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // SSH Port (optional, default: 22) |
| 548 | if (args["ssh-port"]) { |
| 549 | config.port = parseInt(args["ssh-port"], 10); |
| 550 | sources.push("ssh-port from command line"); |
| 551 | } else if (process.env.SSH_PORT) { |
| 552 | config.port = parseInt(process.env.SSH_PORT, 10); |
| 553 | sources.push("SSH_PORT from environment"); |
| 554 | } |
| 555 | |
| 556 | // SSH User (required) |
| 557 | if (args["ssh-user"]) { |
| 558 | config.username = args["ssh-user"]; |
| 559 | sources.push("ssh-user from command line"); |
| 560 | } else if (process.env.SSH_USER) { |
| 561 | config.username = process.env.SSH_USER; |
| 562 | sources.push("SSH_USER from environment"); |
| 563 | } |
no test coverage detected