@@ -107,17 +107,21 @@ export class SSHConfig {
107107 // old configs
108108 this . cleanUpOldConfig ( )
109109 const block = this . getBlock ( )
110+ const newBlock = this . buildBlock ( values , overrides )
110111 if ( block ) {
111- this . eraseBlock ( block )
112+ this . replaceBlock ( block , newBlock )
113+ } else {
114+ this . appendBlock ( newBlock )
112115 }
113- this . appendBlock ( values , overrides )
114116 await this . save ( )
115117 }
116118
117119 private async cleanUpOldConfig ( ) {
118120 const raw = this . getRaw ( )
119121 const oldConfig = raw . split ( "\n\n" ) . find ( ( config ) => config . startsWith ( "Host coder-vscode--*" ) )
120- if ( oldConfig ) {
122+ // Perform additional sanity check that the block also contains a
123+ // ProxyCommand, otherwise it might be a different block.
124+ if ( oldConfig && oldConfig . includes ( " ProxyCommand " ) ) {
121125 this . raw = raw . replace ( oldConfig , "" )
122126 }
123127 }
@@ -149,13 +153,8 @@ export class SSHConfig {
149153 }
150154 }
151155
152- private eraseBlock ( block : Block ) {
153- this . raw = this . getRaw ( ) . replace ( block . raw , "" )
154- }
155-
156156 /**
157- *
158- * appendBlock builds the ssh config block. The order of the keys is determinstic based on the input.
157+ * buildBlock builds the ssh config block. The order of the keys is determinstic based on the input.
159158 * Expected values are always in a consistent order followed by any additional overrides in sorted order.
160159 *
161160 * @param param0 - SSHValues are the expected SSH values for using ssh with coder.
@@ -164,7 +163,7 @@ export class SSHConfig {
164163 * If the key matches an expected value, the expected value is overridden. If it does not
165164 * match an expected value, it is appended to the end of the block.
166165 */
167- private appendBlock ( { Host, ...otherValues } : SSHValues , overrides : Record < string , string > ) {
166+ private buildBlock ( { Host, ...otherValues } : SSHValues , overrides : Record < string , string > ) : Block {
168167 const lines = [ this . startBlockComment , `Host ${ Host } ` ]
169168
170169 // configValues is the merged values of the defaults and the overrides.
@@ -180,12 +179,22 @@ export class SSHConfig {
180179 } )
181180
182181 lines . push ( this . endBlockComment )
182+ return {
183+ raw : lines . join ( "\n" ) ,
184+ }
185+ }
186+
187+ private replaceBlock ( oldBlock : Block , newBlock : Block ) {
188+ this . raw = this . getRaw ( ) . replace ( oldBlock . raw , newBlock . raw )
189+ }
190+
191+ private appendBlock ( block : Block ) {
183192 const raw = this . getRaw ( )
184193
185194 if ( this . raw === "" ) {
186- this . raw = lines . join ( "\n" )
195+ this . raw = block . raw
187196 } else {
188- this . raw = `${ raw . trimEnd ( ) } \n\n${ lines . join ( "\n" ) } `
197+ this . raw = `${ raw . trimEnd ( ) } \n\n${ block . raw } `
189198 }
190199 }
191200
0 commit comments