Adding upstream version 0.5.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
6dc19540ee
commit
19551ac12c
23 changed files with 6571 additions and 0 deletions
48
examples/query.rs
Normal file
48
examples/query.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use std::env::args;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::exit;
|
||||
|
||||
use dirs::home_dir;
|
||||
use ssh2_config::{ParseRule, SshConfig};
|
||||
|
||||
fn main() {
|
||||
// get args
|
||||
let args: Vec<String> = args().collect();
|
||||
let address = match args.get(1) {
|
||||
Some(addr) => addr.to_string(),
|
||||
None => {
|
||||
usage();
|
||||
exit(255)
|
||||
}
|
||||
};
|
||||
// check path
|
||||
let config_path = match args.get(2) {
|
||||
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());
|
||||
let params = config.query(address.as_str());
|
||||
println!("Configuration for {}: {:?}", address, params);
|
||||
}
|
||||
|
||||
fn usage() {
|
||||
eprintln!("Usage: cargo run --example query -- <address> [config-path]");
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue