@@ -12,20 +12,24 @@ export async function loadFromPath(p: string): Promise<SSHConfigModule.SSHConfig
1212 return SSHConfig . parse ( s ) ;
1313}
1414
15- export function isDirective ( entry : SSHConfigModule . Config ) : entry is SSHConfigModule . ConfigDirective {
15+ export function isDirective ( entry : any ) : entry is SSHConfigModule . ConfigDirective {
1616 return entry && entry . type === SSHConfig . DIRECTIVE ;
1717}
1818
19+ export function isHostDirective ( entry : SSHConfigModule . Config ) : entry is SSHConfigModule . ConfigHostDirective {
20+ return isDirective ( entry ) && entry . param === 'Host' ;
21+ }
22+
1923export function getConfigPath ( ) {
2024 return path . resolve ( os . homedir ( ) , '.ssh' , 'config' ) ;
2125}
2226
23- export function findHostSection ( conf : SSHConfigModule . SSHConfig , host : string ) : SSHConfigModule . ConfigDirective | null {
27+ export function findHostSection ( conf : SSHConfigModule . SSHConfig , host : string ) : SSHConfigModule . ConfigHostDirective | null {
2428 return conf . find ( { Host : host } ) ;
2529}
2630
2731export function ensureHostAndKeyPath ( conf : SSHConfigModule . SSHConfig , conn : { host : string , port ?: number } , keyPath : string ) : void {
28- const section = ensureSection ( conf , conn . host ) ;
32+ const section = ensureHostSection ( conf , conn . host ) ;
2933 const index = conf . indexOf ( section ) ;
3034
3135 ensureSectionLine ( section , 'IdentityFile' , keyPath ) ;
@@ -41,9 +45,12 @@ export function ensureHostAndKeyPath(conf: SSHConfigModule.SSHConfig, conn: { ho
4145 } else {
4246 const previousSection = conf [ index - 1 ] ;
4347
44- if ( isDirective ( previousSection ) ) {
48+ if ( isHostDirective ( previousSection ) ) {
4549 const previousSectionLastEntry = previousSection . config [ previousSection . config . length - 1 ] ;
46- previousSectionLastEntry . after = '\n' ;
50+
51+ if ( previousSectionLastEntry ) {
52+ previousSectionLastEntry . after = '\n' ;
53+ }
4754 } else {
4855 previousSection . after = '\n' ;
4956 }
@@ -53,6 +60,10 @@ export function ensureHostAndKeyPath(conf: SSHConfigModule.SSHConfig, conn: { ho
5360
5461 section . after = '\n' ;
5562
63+ if ( ! section . config ) {
64+ section . config = [ ] ;
65+ }
66+
5667 for ( let entry of section . config ) {
5768 entry . before = ' ' ;
5869 entry . after = '\n' ;
@@ -64,7 +75,7 @@ export function ensureHostAndKeyPath(conf: SSHConfigModule.SSHConfig, conn: { ho
6475 }
6576}
6677
67- function ensureSection ( conf : SSHConfigModule . SSHConfig , host : string ) : SSHConfigModule . ConfigDirective {
78+ function ensureHostSection ( conf : SSHConfigModule . SSHConfig , host : string ) : SSHConfigModule . ConfigHostDirective {
6879 let section = findHostSection ( conf , host ) ;
6980
7081 if ( ! section ) {
@@ -79,7 +90,7 @@ function ensureSection(conf: SSHConfigModule.SSHConfig, host: string): SSHConfig
7990 return section ;
8091}
8192
82- function ensureSectionLine ( section : SSHConfigModule . ConfigDirective , key : string , value : string ) : void {
93+ function ensureSectionLine ( section : SSHConfigModule . ConfigHostDirective , key : string , value : string ) : void {
8394 const found = section . config . some ( line => {
8495 if ( isDirective ( line ) ) {
8596 if ( line . param === key ) {
0 commit comments