use shy::App;
use std::{io, os::unix::process::CommandExt, panic, process::Command};
fn main() -> Result<(), io::Error> {
let args = parse_args()?;
for arg in args {
match arg.as_ref() {
"-h" | "-help" | "--help" => return print_usage(),
"-v" | "-version" | "--version" => return print_version(),
"-c" | "-config" | "--config" => todo!(),
_ => {}
}
}
if let Some(hostname) = run()? {
std::env::set_var("TERM", "xterm"); let mut cmd = Command::new("ssh");
let cmd = cmd.arg(hostname);
let err = cmd.exec();
eprintln!("{:?}", err);
}
Ok(())
}
fn run() -> Result<Option<String>, io::Error> {
setup_panic_hook();
let mut app = App::new()?;
Ok(app.run()?)
}
fn setup_panic_hook() {
panic::set_hook(Box::new(|panic_info| {
println!("{}", panic_info);
}));
}
fn parse_args() -> Result<Vec<String>, io::Error> {
let mut args = vec![];
for arg in std::env::args().skip(1).collect::<Vec<String>>() {
if arg.starts_with('-') && arg.contains('=') {
for part in arg.split("=") {
args.push(part.to_string());
}
} else {
args.push(arg);
}
}
Ok(args)
}
fn print_usage() -> Result<(), io::Error> {
println!(
"usage: shy [options]
Options:
-c, --config FILE Use config file of ~/.ssh/config
-v, --version Print shy version and exit.
-h, --help Show this message."
);
Ok(())
}
fn print_version() -> Result<(), io::Error> {
println!("shy v{}", shy::VERSION);
Ok(())
}