Skip to content

Commit

Permalink
feat(ssh_config): support glob include paths
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael DEMACON <[email protected]>
  • Loading branch information
quantumsheep committed Feb 22, 2024
1 parent 19a60c2 commit c742bde
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow = "1.0.80"
clap = { version = "4.5.0", features = ["derive"] }
crossterm = "0.27.0"
fuzzy-matcher = "0.3.7"
glob = "0.3.1"
handlebars = "5.1.0"
itertools = "0.12.1"
ratatui = "0.26.1"
Expand Down
49 changes: 26 additions & 23 deletions src/ssh_config/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::anyhow;
use anyhow::Result;
use glob::glob;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
Expand Down Expand Up @@ -91,30 +92,32 @@ impl Parser {
include_path = format!("{ssh_config_directory}/{include_path}");
}

let path = std::fs::canonicalize(include_path)?
.to_str()
.ok_or(anyhow!("Failed to convert path to string"))?
.to_string();

let mut file = BufReader::new(File::open(path)?);
let (included_global_host, included_hosts) = self.parse_raw(&mut file)?;

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"));
for path in glob(&include_path)? {
let path = match path {
Ok(path) => path,
Err(e) => return Err(anyhow!("Failed to glob path: {e}")),
};

let mut file = BufReader::new(File::open(path)?);
let (included_global_host, included_hosts) = self.parse_raw(&mut file)?;

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

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

hosts
.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;
Expand Down

0 comments on commit c742bde

Please sign in to comment.