( opts: EnsureMuxCoderSSHConfigFileOptions )
| 220 | } |
| 221 | |
| 222 | export async function ensureMuxCoderSSHConfigFile( |
| 223 | opts: EnsureMuxCoderSSHConfigFileOptions |
| 224 | ): Promise<void> { |
| 225 | const configuredSSHConfigPath = opts.sshConfigPath ?? path.join(os.homedir(), ".ssh", "config"); |
| 226 | // Preserve users' symlinked ~/.ssh/config setups by writing to the symlink target, |
| 227 | // rather than replacing the symlink path itself. |
| 228 | const sshConfigPath = await resolveSSHConfigWritePath(configuredSSHConfigPath); |
| 229 | const sshConfigDir = path.dirname(sshConfigPath); |
| 230 | |
| 231 | await fs.mkdir(sshConfigDir, { recursive: true, mode: 0o700 }); |
| 232 | |
| 233 | const { content: existingContent, mode: existingMode } = |
| 234 | await loadSSHConfigContent(sshConfigPath); |
| 235 | |
| 236 | const startMarkerCount = countOccurrences(existingContent, MUX_CODER_SSH_BLOCK_START); |
| 237 | const endMarkerCount = countOccurrences(existingContent, MUX_CODER_SSH_BLOCK_END); |
| 238 | |
| 239 | if (startMarkerCount > 1 || endMarkerCount > 1) { |
| 240 | throw new Error("Corrupted SSH config: duplicate Mux Coder SSH markers detected."); |
| 241 | } |
| 242 | |
| 243 | if (startMarkerCount !== endMarkerCount) { |
| 244 | throw new Error("Corrupted SSH config: mismatched Mux Coder SSH markers detected."); |
| 245 | } |
| 246 | |
| 247 | const nextMuxBlock = renderMuxBlock(opts.coderBinaryPath); |
| 248 | const nextContent = |
| 249 | startMarkerCount === 1 |
| 250 | ? replaceMuxBlock(existingContent, nextMuxBlock) |
| 251 | : appendMuxBlock(existingContent, nextMuxBlock); |
| 252 | |
| 253 | await writeConfigAtomically(sshConfigPath, nextContent, existingMode); |
| 254 | } |
no test coverage detected