use clap::{Args, Parser}; use tracing::{metadata::LevelFilter, Level}; use crate::{history, session, stdio, tmux}; #[derive(Debug, Clone, Parser)] pub struct Config { #[command(flatten)] pub enabled: Flags, #[command(flatten)] pub stdio: stdio::Config, #[command(flatten)] pub sessions: session::Config, #[command(flatten)] pub history: history::History, #[command(flatten)] pub tmux: tmux::Tmux, #[command(flatten)] pub verbosity: Verbosity, } #[derive(Debug, Clone, Args)] pub struct Flags { /// Include localhost #[arg(short, long)] pub localhost: bool, /// Include hosts from tmux session names #[arg(short, long)] pub tmux: bool, /// Include hosts from history file #[arg(short = 'H', long)] pub history: bool, /// Include hosts from the ssh `known_hosts` #[arg(short, long)] pub ssh: bool, /// Include hosts from `/etc/hosts` #[arg(short = 'o', long)] pub hosts: bool, /// Alias to include all host sources #[arg(short, long)] pub all: bool, } impl Flags { pub fn localhost(&self) -> bool { self.all || self.localhost } pub fn tmux(&self) -> bool { self.all || self.tmux } pub fn history(&self) -> bool { self.all || self.history } pub fn ssh(&self) -> bool { self.all || self.ssh } pub fn hosts(&self) -> bool { self.all || self.hosts } } #[derive(Debug, Default, Clone, Args)] pub struct Verbosity { /// Print additional information per occurrence. /// /// Conflicts with `--quiet`. #[arg(short, long, global = true, action = clap::ArgAction::Count, conflicts_with = "quiet")] pub verbose: u8, /// Suppress all output. /// /// Conflicts with `--verbose`. #[arg(short, long, global = true, conflicts_with = "verbose")] pub quiet: bool, } impl From<&Verbosity> for Option { fn from(value: &Verbosity) -> Self { match 1 + value.verbose - u8::from(value.quiet) { 0 => None, 1 => Some(Level::ERROR), 2 => Some(Level::WARN), 3 => Some(Level::INFO), 4 => Some(Level::DEBUG), _ => Some(Level::TRACE), } } } impl From<&Verbosity> for LevelFilter { fn from(value: &Verbosity) -> Self { Option::::from(value).into() } }