From c94960b49f463b3c4a7da9fb1b6b2c122f7dd125 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Mon, 3 Apr 2023 19:12:14 -0500 Subject: refactor: clean up logic and impl trait for writing sessions --- src/main.rs | 73 +++++++++++++++++++++++++++---------------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index eb62d52..523ee82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,62 +1,53 @@ -use std::io::ErrorKind; - +use anyhow::Context; use clap::Parser; -use sshr::{Config, History, Hosts, KnownHosts, Sessions, Tmux}; +use sshr::{Config, HostName, Hosts, KnownHosts, Sessions, Stdout}; fn main() -> anyhow::Result<()> { - let mut config = Config::parse(); + let config = Config::parse(); tracing_subscriber::fmt::fmt() .with_max_level(&config.verbosity) .without_time() .init(); - config.history_file = config.history_file.or_else(History::default_path); - - let history = match config.history_file { - Some(path) => History::open(path), - None => Err(std::io::Error::from(ErrorKind::NotFound)), - }; - - let tmux = Tmux::new(config.socket_name); + let mut sessions = Sessions::new(config.sessions); - let mut sessions = Sessions::new(config.exclude); + if config.enabled.localhost() { + let hostname = HostName::get().context("Failed to get hostname of localhost")?; + sessions.extend(hostname); + } - match tmux.host() { - Ok(p) => sessions.add(p), - Err(err) => tracing::warn!(?err, "Failed to get tmux host"), - }; + if config.enabled.tmux() { + let tmux_sessions = config.tmux.list().context("Failed to list tmux sessions")?; + sessions.extend(tmux_sessions); + } - match tmux.list(None) { - Ok(p) => sessions.extend(p), - Err(err) => tracing::warn!(?err, "Failed to list tmux sessions"), - }; + if config.enabled.ssh() { + let known_hosts = KnownHosts::open().context("Failed to read KnownHost file")?; + sessions.extend(known_hosts); + } - match history { - Ok(p) => { - sessions.extend(p.entries); + if config.enabled.hosts() { + let hosts = Hosts::open().context("Failed to read /etc/hosts")?; + sessions.extend(hosts); + } - if config.update { - sessions.write_into(p.file)?; + if config.enabled.history() { + match config.history.read() { + Ok(h) => sessions.extend(h), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + tracing::warn!("Skipping non-existant history file") } + Err(err) => return Err(err).context("Failed to read history file"), } - Err(err) => tracing::warn!(?err, "Failed to open history file"), - }; - - match KnownHosts::open() { - Ok(p) => sessions.extend(p), - Err(err) => tracing::warn!(?err, "Failed to read KnownHost file"), - }; - - match Hosts::open() { - Ok(p) => sessions.extend(p), - Err(err) => tracing::warn!(?err, "Failed to read /etc/hosts"), - }; + } - for session in sessions.sorted() { - println!("{session}"); + if config.history.update { + sessions.write_sessions(config.history)?; } - Ok(()) + sessions + .write_sessions(Stdout::new(config.stdio)) + .context("Failed to write to stdout") } -- cgit v1.2.3-70-g09d2