summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 258255b86c322c47a8dfd4dccdebaa5843bdce05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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<Level> {
    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::<Level>::from(value).into()
    }
}