@@ -107,17 +107,21 @@ export class SSHConfig {
107
107
// old configs
108
108
this . cleanUpOldConfig ( )
109
109
const block = this . getBlock ( )
110
+ const newBlock = this . buildBlock ( values , overrides )
110
111
if ( block ) {
111
- this . eraseBlock ( block )
112
+ this . replaceBlock ( block , newBlock )
113
+ } else {
114
+ this . appendBlock ( newBlock )
112
115
}
113
- this . appendBlock ( values , overrides )
114
116
await this . save ( )
115
117
}
116
118
117
119
private async cleanUpOldConfig ( ) {
118
120
const raw = this . getRaw ( )
119
121
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 " ) ) {
121
125
this . raw = raw . replace ( oldConfig , "" )
122
126
}
123
127
}
@@ -149,13 +153,8 @@ export class SSHConfig {
149
153
}
150
154
}
151
155
152
- private eraseBlock ( block : Block ) {
153
- this . raw = this . getRaw ( ) . replace ( block . raw , "" )
154
- }
155
-
156
156
/**
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.
159
158
* Expected values are always in a consistent order followed by any additional overrides in sorted order.
160
159
*
161
160
* @param param0 - SSHValues are the expected SSH values for using ssh with coder.
@@ -164,7 +163,7 @@ export class SSHConfig {
164
163
* If the key matches an expected value, the expected value is overridden. If it does not
165
164
* match an expected value, it is appended to the end of the block.
166
165
*/
167
- private appendBlock ( { Host, ...otherValues } : SSHValues , overrides : Record < string , string > ) {
166
+ private buildBlock ( { Host, ...otherValues } : SSHValues , overrides : Record < string , string > ) : Block {
168
167
const lines = [ this . startBlockComment , `Host ${ Host } ` ]
169
168
170
169
// configValues is the merged values of the defaults and the overrides.
@@ -180,12 +179,22 @@ export class SSHConfig {
180
179
} )
181
180
182
181
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 ) {
183
192
const raw = this . getRaw ( )
184
193
185
194
if ( this . raw === "" ) {
186
- this . raw = lines . join ( "\n" )
195
+ this . raw = block . raw
187
196
} else {
188
- this . raw = `${ raw . trimEnd ( ) } \n\n${ lines . join ( "\n" ) } `
197
+ this . raw = `${ raw . trimEnd ( ) } \n\n${ block . raw } `
189
198
}
190
199
}
191
200
0 commit comments