1
0
Fork 0
rust-ssh2-config/examples/print.rs
Daniel Baumann 19551ac12c
Adding upstream version 0.5.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-06 06:52:03 +02:00

36 lines
998 B
Rust

use std::env::args;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use dirs::home_dir;
use ssh2_config::{ParseRule, SshConfig};
fn main() {
// get args
let args: Vec<String> = args().collect();
// check path
let config_path = match args.get(1) {
Some(p) => PathBuf::from(p),
None => {
let mut p = home_dir().expect("Failed to get home_dir for guest OS");
p.extend(Path::new(".ssh/config"));
p
}
};
// Open config file
let config = read_config(config_path.as_path());
println!("{config}");
}
fn read_config(p: &Path) -> SshConfig {
let mut reader = match File::open(p) {
Ok(f) => BufReader::new(f),
Err(err) => panic!("Could not open file '{}': {}", p.display(), err),
};
match SshConfig::default().parse(&mut reader, ParseRule::STRICT) {
Ok(config) => config,
Err(err) => panic!("Failed to parse configuration: {}", err),
}
}