use std::{ collections::HashSet, fs::File, io::{BufRead, BufReader}, }; use directories::UserDirs; use crate::{Session, SessionSource}; pub struct KnownHosts; impl KnownHosts { pub fn new() -> Self { Self } } impl SessionSource for KnownHosts { type Error = std::io::Error; type Iter = Vec; fn sessions(&self) -> Result { let path = UserDirs::new() .ok_or(std::io::ErrorKind::NotFound)? .home_dir() .join(".ssh/known_hosts"); let reader = BufReader::new(File::open(path)?); let sessions: Vec = reader .lines() .flatten() .filter_map(|s| s.split_whitespace().next().map(str::to_owned)) .collect::>() .into_iter() .map(Session::from) .collect(); Ok(sessions) } } impl Default for KnownHosts { fn default() -> Self { Self::new() } }