Skip to main content

rusty_sweeper/
cli.rs

1use clap::{Args, Parser};
2use std::path::PathBuf;
3
4/// Rusty Sweeper - A Linux disk usage management utility
5#[derive(Parser, Debug)]
6#[command(name = "rusty-sweeper")]
7#[command(author, version, about, long_about = None)]
8#[command(propagate_version = true)]
9pub struct Cli {
10    /// Directory to scan (defaults to the current working directory)
11    #[arg(value_name = "PATH")]
12    pub path: Option<PathBuf>,
13
14    /// Path to configuration file
15    #[arg(short, long, global = true, value_name = "PATH")]
16    pub config: Option<PathBuf>,
17
18    /// Increase verbosity (-v, -vv, -vvv)
19    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
20    pub verbose: u8,
21
22    /// Suppress non-essential output
23    #[arg(short, long, global = true)]
24    pub quiet: bool,
25}
26
27/// Resolve the TUI's starting path, defaulting to the process working directory.
28pub fn starting_path(path: Option<PathBuf>) -> std::io::Result<PathBuf> {
29    match path {
30        Some(path) => Ok(path),
31        None => std::env::current_dir(),
32    }
33}
34
35/// Dedicated CLI for the monitor binary.
36#[derive(Parser, Debug)]
37#[command(name = "rusty-sweeper-monitor")]
38#[command(
39    author,
40    version,
41    about = "Disk usage monitor daemon and alerting service"
42)]
43#[command(propagate_version = true)]
44pub struct MonitorCli {
45    /// Path to configuration file
46    #[arg(short, long, global = true, value_name = "PATH")]
47    pub config: Option<PathBuf>,
48
49    /// Increase verbosity (-v, -vv, -vvv)
50    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
51    pub verbose: u8,
52
53    /// Suppress non-essential output
54    #[arg(short, long, global = true)]
55    pub quiet: bool,
56
57    #[command(flatten)]
58    pub args: MonitorArgs,
59}
60
61#[derive(Args, Debug)]
62pub struct MonitorArgs {
63    /// Run as background daemon
64    #[arg(short, long)]
65    pub daemon: bool,
66
67    /// Check interval in seconds
68    #[arg(short, long, default_value = "300", value_name = "SECS")]
69    pub interval: u64,
70
71    /// Warning threshold percentage
72    #[arg(short, long, default_value = "80", value_name = "PERCENT")]
73    pub warn: u8,
74
75    /// Critical threshold percentage
76    #[arg(short = 'C', long, default_value = "90", value_name = "PERCENT")]
77    pub critical: u8,
78
79    /// Mount points to monitor (can be specified multiple times)
80    #[arg(short, long, value_name = "PATH")]
81    pub mount: Vec<PathBuf>,
82
83    /// Check once and exit
84    #[arg(long)]
85    pub once: bool,
86
87    /// Stop running daemon
88    #[arg(long)]
89    pub stop: bool,
90
91    /// Show daemon status
92    #[arg(long)]
93    pub status: bool,
94
95    /// Notification backend (auto, dbus, notify-send, stderr)
96    #[arg(long, default_value = "auto", value_name = "BACKEND")]
97    pub notify: String,
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use clap::CommandFactory;
104
105    #[test]
106    fn verify_cli_structure() {
107        // Validates the CLI definition is correct
108        Cli::command().debug_assert();
109    }
110
111    #[test]
112    fn parse_main_cli_without_subcommands() {
113        let cli = Cli::parse_from(["rusty-sweeper"]);
114        assert!(cli.path.is_none());
115        assert_eq!(
116            starting_path(cli.path).unwrap(),
117            std::env::current_dir().unwrap()
118        );
119        assert!(cli.config.is_none());
120        assert!(!cli.quiet);
121    }
122
123    #[test]
124    fn parse_main_cli_with_starting_path() {
125        let cli = Cli::parse_from(["rusty-sweeper", "/tmp/projects"]);
126        assert_eq!(cli.path, Some(PathBuf::from("/tmp/projects")));
127        assert_eq!(
128            starting_path(cli.path).unwrap(),
129            PathBuf::from("/tmp/projects")
130        );
131    }
132
133    #[test]
134    fn parse_main_cli_with_globals() {
135        let cli = Cli::parse_from(["rusty-sweeper", "--quiet", "--config", "/tmp/test.toml"]);
136        assert!(cli.quiet);
137        assert_eq!(cli.config, Some(PathBuf::from("/tmp/test.toml")));
138    }
139
140    #[test]
141    fn global_verbose_flag() {
142        let cli = Cli::parse_from(["rusty-sweeper", "-vvv"]);
143        assert_eq!(cli.verbose, 3);
144    }
145
146    #[test]
147    fn parse_monitor_cli() {
148        let cli = MonitorCli::parse_from(["rusty-sweeper-monitor", "--once", "--notify", "stderr"]);
149        assert!(cli.args.once);
150        assert_eq!(cli.args.notify, "stderr");
151    }
152}