Skip to content

Commit

Permalink
Merge 5469cf2 into 36bd55e
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-ardi committed Jul 28, 2023
2 parents 36bd55e + 5469cf2 commit ef60860
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: sudo apt update && sudo apt install -y libssh2-1-dev libssl-dev
run: sudo apt update && sudo apt install -y libssh2-1-dev libssl-dev && mkdir -p $HOME/.ssh && touch $HOME/.ssh/config
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: sudo apt update && sudo apt install -y libssh2-1-dev libssl-dev
run: sudo apt update && sudo apt install -y libssh2-1-dev libssl-dev && mkdir -p $HOME/.ssh && touch $HOME/.ssh/config
- name: Setup nightly toolchain
uses: actions-rs/toolchain@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group_imports = "StdExternalCrate"
imports_granularity = "Module"
43 changes: 42 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@

#![doc(html_playground_url = "https://play.rust-lang.org")]

use std::io::BufRead;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;
use std::time::Duration;
// -- modules
Expand Down Expand Up @@ -110,6 +111,24 @@ impl SshConfig {
parser::SshConfigParser::parse(&mut self, reader, rules).map(|_| self)
}

#[cfg(target_family = "unix")]
/// Parse ~/.ssh/config file and return parsed configuration or parser error
pub fn parse_default_file(rules: ParseRule) -> SshParserResult<Self> {
let ssh_folder = dirs::home_dir()
.ok_or_else(|| {
SshParserError::Io(io::Error::new(
io::ErrorKind::NotFound,
"Home folder not found",
))
})?
.join(".ssh");

let mut reader =
BufReader::new(File::open(ssh_folder.join("config")).map_err(SshParserError::Io)?);

Self::default().parse(&mut reader, rules)
}

pub fn get_hosts(&self) -> &Vec<Host> {
&self.hosts
}
Expand All @@ -130,6 +149,28 @@ mod test {
assert_eq!(config.query("192.168.1.2"), HostParams::default());
}

#[test]
#[cfg(target_family = "unix")]
fn should_parse_default_config() {
assert!(SshConfig::parse_default_file(ParseRule::ALLOW_UNKNOWN_FIELDS).is_ok());
}

#[test]
fn should_parse_config() {
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

let mut reader = BufReader::new(
File::open(Path::new("./assets/ssh.config"))
.expect("Could not open configuration file"),
);

assert!(SshConfig::default()
.parse(&mut reader, ParseRule::STRICT)
.is_ok());
}

#[test]
fn should_query_ssh_config() {
let mut config = SshConfig::default();
Expand Down

0 comments on commit ef60860

Please sign in to comment.