From 19a60c2907441e28b1cfbd369ce82cf0b46fabe6 Mon Sep 17 00:00:00 2001 From: Nathanael DEMACON Date: Thu, 22 Feb 2024 12:04:59 +0100 Subject: [PATCH] fix(ssh_config): scope host blocks by parsed file Closes #67 Signed-off-by: Nathanael DEMACON --- src/ssh_config/parser.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ssh_config/parser.rs b/src/ssh_config/parser.rs index b7c860e..1591325 100644 --- a/src/ssh_config/parser.rs +++ b/src/ssh_config/parser.rs @@ -56,6 +56,7 @@ impl Parser { fn parse_raw(&self, reader: &mut impl BufRead) -> Result<(Host, Vec)> { let mut global_host = Host::new(Vec::new()); + let mut is_in_host_block = false; let mut hosts = Vec::new(); let mut line = String::new(); @@ -78,6 +79,7 @@ impl Parser { EntryType::Host => { let patterns = parse_patterns(&entry.1); hosts.push(Host::new(patterns)); + is_in_host_block = true; continue; } @@ -97,13 +99,7 @@ impl Parser { let mut file = BufReader::new(File::open(path)?); let (included_global_host, included_hosts) = self.parse_raw(&mut file)?; - if hosts.is_empty() { - if !included_global_host.is_empty() { - global_host.extend_entries(&included_global_host); - } - - hosts.extend(included_hosts); - } else { + if is_in_host_block { // Can't include hosts inside a host block if !included_hosts.is_empty() { return Err(anyhow!("Cannot include hosts inside a host block")); @@ -113,6 +109,12 @@ impl Parser { .last_mut() .unwrap() .extend_entries(&included_global_host); + } else { + if !included_global_host.is_empty() { + global_host.extend_entries(&included_global_host); + } + + hosts.extend(included_hosts); } continue; @@ -120,10 +122,10 @@ impl Parser { _ => {} } - if hosts.is_empty() { - global_host.update(entry); - } else { + if is_in_host_block { hosts.last_mut().unwrap().update(entry); + } else { + global_host.update(entry); } }