diff --git a/ssh-rs/src/main.rs b/ssh-rs/src/main.rs index db80eb2..ba6fb64 100644 --- a/ssh-rs/src/main.rs +++ b/ssh-rs/src/main.rs @@ -1,7 +1,7 @@ use clap::Parser; use rust_util::{ - debugging, failure_and_exit, iff, information, opt_result, simple_error, success, util_cmd, - util_env, util_file, util_term, XResult, + XResult, debugging, failure_and_exit, iff, information, opt_result, simple_error, success, + util_cmd, util_env, util_file, util_term, }; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; @@ -65,79 +65,14 @@ fn main() -> XResult<()> { let username_and_host = match args.username_and_host { None => { - success!("Total {} server(s):", ssh_rs_config.profiles.len()); - let mut max_profile_id_len = 0_usize; - let mut max_host_len = 0_usize; - for (profile_id, profile) in &ssh_rs_config.profiles { - if profile_id.len() > max_profile_id_len { - max_profile_id_len = profile_id.len(); - } - if profile.host.len() > max_host_len { - max_host_len = profile.host.len(); - } - } - for (profile_id, profile) in &ssh_rs_config.profiles { - let features = make_features( - profile.forward_agent.unwrap_or(false), - profile.proxy.unwrap_or(false), - ); - println!( - "- {} : {}{} {}{}{} # {}{}", - pad(profile_id, max_profile_id_len), - util_term::GREEN, - pad(&profile.host, max_host_len), - util_term::YELLOW, - match &profile.alias { - None => { - "".to_string() - } - Some(alias) => { - format!( - "alias{}: [{}]", - iff!(alias.len() == 1, " ", "es"), - alias.join(", ") - ) - } - }, - util_term::END, - profile.comment.clone().unwrap_or_else(|| "-".to_string()), - iff!( - features.is_empty(), - "".to_string(), - format!(" ;[{}]", features.join(", ")) - ) - ); - } - - let features = make_features( - ssh_rs_config.default_forward_agent.unwrap_or(false), - ssh_rs_config.default_proxy.unwrap_or(false), - ); - if !features.is_empty() { - println!(); - information!("Global default features: [{}]", features.join(", ")); - } - + print_when_no_args(&ssh_rs_config); return Ok(()); } Some(username_and_host) => username_and_host, }; let (username, host) = parse_username_and_host(&username_and_host)?; - - let profiles = ssh_rs_config.find_profiles(&host); - if profiles.is_empty() { - return simple_error!("Profile not found"); - } - if profiles.len() > 1 { - let profile_names = profiles - .iter() - .map(|p| p.0.to_string()) - .collect::>() - .join(", "); - return simple_error!("Multiple profiles found: {}", profile_names); - } - let profile = profiles[0].1; + let profile = match_profile(&ssh_rs_config, &host)?; debugging!("Found profile: {:#?}", profile); @@ -190,6 +125,77 @@ fn main() -> XResult<()> { Ok(()) } +fn match_profile<'a>(ssh_rs_config: &'a SshConfig, host: &String) -> XResult<&'a SshProfile> { + let profiles = ssh_rs_config.find_profiles(&host); + if profiles.is_empty() { + return simple_error!("Profile not found"); + } + if profiles.len() > 1 { + let profile_names = profiles + .iter() + .map(|p| p.0.to_string()) + .collect::>() + .join(", "); + return simple_error!("Multiple profiles found: {}", profile_names); + } + Ok(profiles[0].1) +} + +fn print_when_no_args(ssh_rs_config: &SshConfig) { + success!("Total {} server(s):", ssh_rs_config.profiles.len()); + let mut max_profile_id_len = 0_usize; + let mut max_host_len = 0_usize; + for (profile_id, profile) in &ssh_rs_config.profiles { + if profile_id.len() > max_profile_id_len { + max_profile_id_len = profile_id.len(); + } + if profile.host.len() > max_host_len { + max_host_len = profile.host.len(); + } + } + for (profile_id, profile) in &ssh_rs_config.profiles { + let features = make_features( + profile.forward_agent.unwrap_or(false), + profile.proxy.unwrap_or(false), + ); + println!( + "- {} : {}{} {}{}{} # {}{}", + pad(profile_id, max_profile_id_len), + util_term::GREEN, + pad(&profile.host, max_host_len), + util_term::YELLOW, + match &profile.alias { + None => { + "".to_string() + } + Some(alias) => { + format!( + "alias{}: [{}]", + iff!(alias.len() == 1, " ", "es"), + alias.join(", ") + ) + } + }, + util_term::END, + profile.comment.clone().unwrap_or_else(|| "-".to_string()), + iff!( + features.is_empty(), + "".to_string(), + format!(" ;[{}]", features.join(", ")) + ) + ); + } + + let features = make_features( + ssh_rs_config.default_forward_agent.unwrap_or(false), + ssh_rs_config.default_proxy.unwrap_or(false), + ); + if !features.is_empty() { + println!(); + information!("Global default features: [{}]", features.join(", ")); + } +} + fn pad(str: &str, width: usize) -> String { if str.len() >= width { return str.to_string();