summaryrefslogtreecommitdiffstats
path: root/src/cli.rs
blob: 6f7ee5d1a9df0e0cb61f8fdc5068316f17d81363 (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
use clap::{Args, Parser};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tracing::{metadata::LevelFilter, Level};

use crate::{config::SearchEntryConfig, Config};

/// Tool for listing project directories.
#[derive(Debug, Clone, Default, Parser, Serialize, Deserialize)]
#[command(author, version, about)]
pub struct Cli {
    #[command(flatten)]
    pub projects: Projects,

    #[command(flatten)]
    pub verbosity: Verbosity,
}

#[derive(Debug, Default, Clone, Args, Serialize, Deserialize)]
#[serde(into = "Config")]
pub struct Projects {
    /// Directory to search.
    ///
    /// Directories are searched recursively based on `--max-depth`.
    paths: Vec<PathBuf>,

    /// Max depth to recurse.
    ///
    /// Setting to 0 will only use the supplied directory.
    #[arg(short = 'd', long, default_value = "1")]
    max_depth: Option<usize>,

    /// Recurse into hidden directories.
    ///
    /// Traverse into hidden directories while searching. A directory is considered hidden
    /// if its name starts with a `.` sign (dot). If `--max-depth` is 0, this has no effect.
    #[arg(long)]
    hidden: bool,

    /// Match directories containing item named <PATTERN>
    #[arg(long, short)]
    pattern: Option<String>,

    /// Match git repositories
    #[cfg(feature = "git")]
    #[arg(long, short, default_value_t = true)]
    git: bool,
}

impl From<Projects> for Config {
    fn from(value: Projects) -> Self {
        let paths = value
            .paths
            .iter()
            .cloned()
            .map(|path_buf| SearchEntryConfig {
                path_buf,
                hidden: value.hidden,
                max_depth: value.max_depth,
                pattern: value.pattern.to_owned(),

                #[cfg(feature = "git")]
                git: value.git,
            })
            .collect();

        Config { paths }
    }
}

#[derive(Debug, Default, Clone, Args, Serialize, Deserialize)]
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()
    }
}