1use clap::{Args, Parser};
2use std::path::PathBuf;
3
4#[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 #[arg(value_name = "PATH")]
12 pub path: Option<PathBuf>,
13
14 #[arg(short, long, global = true, value_name = "PATH")]
16 pub config: Option<PathBuf>,
17
18 #[arg(short, long, global = true, action = clap::ArgAction::Count)]
20 pub verbose: u8,
21
22 #[arg(short, long, global = true)]
24 pub quiet: bool,
25}
26
27pub 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#[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 #[arg(short, long, global = true, value_name = "PATH")]
47 pub config: Option<PathBuf>,
48
49 #[arg(short, long, global = true, action = clap::ArgAction::Count)]
51 pub verbose: u8,
52
53 #[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 #[arg(short, long)]
65 pub daemon: bool,
66
67 #[arg(short, long, default_value = "300", value_name = "SECS")]
69 pub interval: u64,
70
71 #[arg(short, long, default_value = "80", value_name = "PERCENT")]
73 pub warn: u8,
74
75 #[arg(short = 'C', long, default_value = "90", value_name = "PERCENT")]
77 pub critical: u8,
78
79 #[arg(short, long, value_name = "PATH")]
81 pub mount: Vec<PathBuf>,
82
83 #[arg(long)]
85 pub once: bool,
86
87 #[arg(long)]
89 pub stop: bool,
90
91 #[arg(long)]
93 pub status: bool,
94
95 #[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 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}