rcc/src/cli/command.rs

141 lines
3.7 KiB
Rust

use clap::{Parser, Subcommand};
/// Tool collection for work at RCC.
/// Make sure have some config files in your config folder .config/rcc/
/// with live and stage as environment. Containing DB_USER, DB_PASSWORD, DB_LOCATION, DB_PORT.
#[derive(Parser)]
#[command(name = "RCC")]
#[command(author = "Mathias Rothenhaeusler")]
#[command(version = "1.0")]
pub struct Cli {
#[arg(short = 'e', long = "env")]
pub env: Option<String>,
#[arg(short = 't', long = "target")]
pub target: Option<String>,
#[command(subcommand)]
pub mode: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Merch {
// #[arg(short = 's', long = "search")]
search: Option<String>,
},
Schema {
// #[arg(short = 's', long = "search")]
search: Option<String>,
},
Filter {
filter_id: usize,
#[arg(short, long)]
all: bool,
#[arg(short, long)]
config: bool,
#[arg(short, long)]
log: bool,
},
Page {
// #[arg(short, long)]
page_id: usize,
},
Job {
job_id: usize,
#[arg(short, long)]
log: bool,
},
Use {
file_name: String,
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
fn parse(args: &[&str]) -> Cli {
Cli::try_parse_from(args).expect("parse failed")
}
#[test]
fn merch_command() {
let cli = parse(&["rcc", "merch", "acme"]);
assert!(matches!(cli.mode, Commands::Merch { search: Some(ref s) } if s == "acme"));
}
#[test]
fn schema_command() {
let cli = parse(&["rcc", "schema", "order_id"]);
assert!(matches!(cli.mode, Commands::Schema { search: Some(ref s) } if s == "order_id"));
}
#[test]
fn filter_command_defaults() {
let cli = parse(&["rcc", "filter", "42"]);
assert!(matches!(
cli.mode,
Commands::Filter { filter_id: 42, all: false, config: false, log: false }
));
}
#[test]
fn filter_command_all_flag() {
let cli = parse(&["rcc", "filter", "42", "--all"]);
assert!(matches!(cli.mode, Commands::Filter { all: true, .. }));
}
#[test]
fn filter_command_config_flag() {
let cli = parse(&["rcc", "filter", "42", "--config"]);
assert!(matches!(cli.mode, Commands::Filter { config: true, .. }));
}
#[test]
fn filter_command_log_flag() {
let cli = parse(&["rcc", "filter", "42", "--log"]);
assert!(matches!(cli.mode, Commands::Filter { log: true, .. }));
}
#[test]
fn page_command() {
let cli = parse(&["rcc", "page", "7"]);
assert!(matches!(cli.mode, Commands::Page { page_id: 7 }));
}
#[test]
fn job_command_default() {
let cli = parse(&["rcc", "job", "3"]);
assert!(matches!(cli.mode, Commands::Job { job_id: 3, log: false }));
}
#[test]
fn job_command_log_flag() {
let cli = parse(&["rcc", "job", "3", "--log"]);
assert!(matches!(cli.mode, Commands::Job { job_id: 3, log: true }));
}
#[test]
fn use_command() {
let cli = parse(&["rcc", "use", "my_module.php"]);
assert!(matches!(cli.mode, Commands::Use { ref file_name } if file_name == "my_module.php"));
}
#[test]
fn env_flag_is_captured() {
let cli = parse(&["rcc", "-e", "stage", "merch", "x"]);
assert_eq!(cli.env.as_deref(), Some("stage"));
}
#[test]
fn env_defaults_to_none_when_not_provided() {
let cli = parse(&["rcc", "merch", "x"]);
assert!(cli.env.is_none());
}
#[test]
fn unknown_subcommand_is_rejected() {
assert!(Cli::try_parse_from(["rcc", "bogus"]).is_err());
}
}