use clap::{Args, Parser};
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(name = "rusty-sweeper")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
#[arg(value_name = "PATH")]
pub path: Option<PathBuf>,
#[arg(short, long, global = true, value_name = "PATH")]
pub config: Option<PathBuf>,
#[arg(short, long, global = true, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(short, long, global = true)]
pub quiet: bool,
}
pub fn starting_path(path: Option<PathBuf>) -> std::io::Result<PathBuf> {
match path {
Some(path) => Ok(path),
None => std::env::current_dir(),
}
}
#[derive(Parser, Debug)]
#[command(name = "rusty-sweeper-monitor")]
#[command(
author,
version,
about = "Disk usage monitor daemon and alerting service"
)]
#[command(propagate_version = true)]
pub struct MonitorCli {
#[arg(short, long, global = true, value_name = "PATH")]
pub config: Option<PathBuf>,
#[arg(short, long, global = true, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(short, long, global = true)]
pub quiet: bool,
#[command(flatten)]
pub args: MonitorArgs,
}
#[derive(Args, Debug)]
pub struct MonitorArgs {
#[arg(short, long)]
pub daemon: bool,
#[arg(short, long, default_value = "300", value_name = "SECS")]
pub interval: u64,
#[arg(short, long, default_value = "80", value_name = "PERCENT")]
pub warn: u8,
#[arg(short = 'C', long, default_value = "90", value_name = "PERCENT")]
pub critical: u8,
#[arg(short, long, value_name = "PATH")]
pub mount: Vec<PathBuf>,
#[arg(long)]
pub once: bool,
#[arg(long)]
pub stop: bool,
#[arg(long)]
pub status: bool,
#[arg(long, default_value = "auto", value_name = "BACKEND")]
pub notify: String,
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn verify_cli_structure() {
Cli::command().debug_assert();
}
#[test]
fn parse_main_cli_without_subcommands() {
let cli = Cli::parse_from(["rusty-sweeper"]);
assert!(cli.path.is_none());
assert_eq!(
starting_path(cli.path).unwrap(),
std::env::current_dir().unwrap()
);
assert!(cli.config.is_none());
assert!(!cli.quiet);
}
#[test]
fn parse_main_cli_with_starting_path() {
let cli = Cli::parse_from(["rusty-sweeper", "/tmp/projects"]);
assert_eq!(cli.path, Some(PathBuf::from("/tmp/projects")));
assert_eq!(
starting_path(cli.path).unwrap(),
PathBuf::from("/tmp/projects")
);
}
#[test]
fn parse_main_cli_with_globals() {
let cli = Cli::parse_from(["rusty-sweeper", "--quiet", "--config", "/tmp/test.toml"]);
assert!(cli.quiet);
assert_eq!(cli.config, Some(PathBuf::from("/tmp/test.toml")));
}
#[test]
fn global_verbose_flag() {
let cli = Cli::parse_from(["rusty-sweeper", "-vvv"]);
assert_eq!(cli.verbose, 3);
}
#[test]
fn parse_monitor_cli() {
let cli = MonitorCli::parse_from(["rusty-sweeper-monitor", "--once", "--notify", "stderr"]);
assert!(cli.args.once);
assert_eq!(cli.args.notify, "stderr");
}
}