summaryrefslogtreecommitdiffstats
path: root/src/cli.rs
blob: 2fd296f088a1b151e4b30011b297d89da57f3f15 (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
use crate::{paths::PathEntry, Config};
use clap::{Args, Parser};
use figment::{value, Metadata, Profile, Provider};
use serde::Serialize;
use std::path::PathBuf;
use tracing::Level;

/// Simple program to manage projects and ssh hosts using tmux
#[derive(Debug, Clone, Default, Parser, Serialize)]
#[command(author, version, about)]
#[serde(into = "Config")]
pub struct Cli {
    /// Path to directories
    pub(crate) paths: Vec<PathBuf>,

    /// Max depth to recurse.
    ///
    /// By default, no limit is set. Setting to 0 will only use the supplied directory.
    #[arg(short = 'd', long)]
    pub(crate) max_depth: Option<usize>,

    /// Recurse into hidden directories.
    ///
    /// Include hidden directories when traversing directories. (default: hidden directories
    /// are skipped). A Directory is considered to be hidden if its name starts with a `.`
    /// sign (dot). If `max-depth` is set to 0, this has no effect (As no recursion happens).
    #[arg(long)]
    pub(crate) hidden: bool,

    #[command(flatten)]
    pub verbose: Verbosity,

    /// Connect to ssh host
    #[arg(short, long)]
    pub ssh: Option<String>,
}

impl Provider for Cli {
    fn metadata(&self) -> Metadata {
        Metadata::named("Tmuxr cli provider")
    }

    fn data(&self) -> figment::error::Result<value::Map<Profile, value::Dict>> {
        Config::from(self.to_owned()).data()
    }
}

impl From<Cli> for Config {
    fn from(value: Cli) -> Self {
        let paths = value
            .paths
            .iter()
            .cloned()
            .map(|p| PathEntry {
                path: p,
                hidden: value.hidden,
                recurse: value.max_depth,
            })
            .collect();

        let paths = crate::paths::Config { paths };
        let logging = crate::logging::Config {
            stdout: value.verbose.into(),
            ..Default::default()
        };

        Config {
            paths,
            logging,
            ..Default::default()
        }
    }
}

#[derive(Debug, Default, Clone, Args)]
pub struct Verbosity {
    /// Print additional information per occurrence
    #[arg(short, long, action = clap::ArgAction::Count, conflicts_with = "quiet")]
    pub verbose: u8,

    /// Suppress all output
    #[arg(short, long, global = true, conflicts_with = "verbose")]
    pub quiet: bool,
}

impl From<Verbosity> for Option<Level> {
    fn from(value: Verbosity) -> Self {
        match value.verbose + 1 - 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),
        }
    }
}