| 138 | } |
| 139 | |
| 140 | func cleanupSSHConfig(instanceID, ipAddress string) error { |
| 141 | homeDir, err := os.UserHomeDir() |
| 142 | if err != nil { |
| 143 | return fmt.Errorf("failed to get home directory: %w", err) |
| 144 | } |
| 145 | |
| 146 | sshConfigPath := filepath.Join(homeDir, ".ssh", "config") |
| 147 | |
| 148 | if err := removeSSHHostEntry(sshConfigPath, instanceID); err != nil { |
| 149 | return fmt.Errorf("failed to clean SSH config: %w", err) |
| 150 | } |
| 151 | |
| 152 | if ipAddress != "" { |
| 153 | cmd := exec.Command("ssh-keygen", "-R", ipAddress) |
| 154 | cmd.Stdout = nil |
| 155 | cmd.Stderr = nil |
| 156 | _ = cmd.Run() //nolint:errcheck // known_hosts cleanup failure is non-fatal |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func removeSSHHostEntry(configPath, instanceID string) error { |
| 163 | if _, err := os.Stat(configPath); os.IsNotExist(err) { |