Skip to content

Commit

Permalink
fix(ssh_config): scope host blocks by parsed file
Browse files Browse the repository at this point in the history
Closes #67

Signed-off-by: Nathanael DEMACON <ndemacon@scaleway.com>
  • Loading branch information
quantumsheep committed Feb 22, 2024
1 parent 853eb92 commit 19a60c2
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/ssh_config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Parser {

fn parse_raw(&self, reader: &mut impl BufRead) -> Result<(Host, Vec<Host>)> {
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();
Expand All @@ -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;
}
Expand All @@ -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"));
Expand All @@ -113,17 +109,23 @@ 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;
}
_ => {}
}

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);
}
}

Expand Down

0 comments on commit 19a60c2

Please sign in to comment.