|
| 1 | +//! CLI argument parsing for Stackdog |
| 2 | +//! |
| 3 | +//! Defines the command-line interface using clap derive macros. |
| 4 | +//! Supports `serve` (HTTP server) and `sniff` (log analysis) subcommands. |
| 5 | +
|
| 6 | +use clap::{Parser, Subcommand}; |
| 7 | + |
| 8 | +/// Stackdog Security — Docker & Linux server security platform |
| 9 | +#[derive(Parser, Debug)] |
| 10 | +#[command(name = "stackdog", version, about, long_about = None)] |
| 11 | +pub struct Cli { |
| 12 | + #[command(subcommand)] |
| 13 | + pub command: Option<Command>, |
| 14 | +} |
| 15 | + |
| 16 | +/// Available subcommands |
| 17 | +#[derive(Subcommand, Debug, Clone)] |
| 18 | +pub enum Command { |
| 19 | + /// Start the HTTP API server (default behavior) |
| 20 | + Serve, |
| 21 | + |
| 22 | + /// Sniff and analyze logs from Docker containers and system sources |
| 23 | + Sniff { |
| 24 | + /// Run a single scan/analysis pass, then exit |
| 25 | + #[arg(long)] |
| 26 | + once: bool, |
| 27 | + |
| 28 | + /// Consume logs: archive to zstd, then purge originals to free disk |
| 29 | + #[arg(long)] |
| 30 | + consume: bool, |
| 31 | + |
| 32 | + /// Output directory for consumed logs |
| 33 | + #[arg(long, default_value = "./stackdog-logs/")] |
| 34 | + output: String, |
| 35 | + |
| 36 | + /// Additional log file paths to watch (comma-separated) |
| 37 | + #[arg(long)] |
| 38 | + sources: Option<String>, |
| 39 | + |
| 40 | + /// Poll interval in seconds |
| 41 | + #[arg(long, default_value = "30")] |
| 42 | + interval: u64, |
| 43 | + |
| 44 | + /// AI provider: "openai" or "candle" |
| 45 | + #[arg(long)] |
| 46 | + ai_provider: Option<String>, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use super::*; |
| 53 | + use clap::Parser; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_no_subcommand_defaults_to_none() { |
| 57 | + let cli = Cli::parse_from(["stackdog"]); |
| 58 | + assert!(cli.command.is_none(), "No subcommand should yield None (default to serve)"); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn test_serve_subcommand() { |
| 63 | + let cli = Cli::parse_from(["stackdog", "serve"]); |
| 64 | + assert!(matches!(cli.command, Some(Command::Serve))); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_sniff_subcommand_defaults() { |
| 69 | + let cli = Cli::parse_from(["stackdog", "sniff"]); |
| 70 | + match cli.command { |
| 71 | + Some(Command::Sniff { once, consume, output, sources, interval, ai_provider }) => { |
| 72 | + assert!(!once); |
| 73 | + assert!(!consume); |
| 74 | + assert_eq!(output, "./stackdog-logs/"); |
| 75 | + assert!(sources.is_none()); |
| 76 | + assert_eq!(interval, 30); |
| 77 | + assert!(ai_provider.is_none()); |
| 78 | + } |
| 79 | + _ => panic!("Expected Sniff command"), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_sniff_with_once_flag() { |
| 85 | + let cli = Cli::parse_from(["stackdog", "sniff", "--once"]); |
| 86 | + match cli.command { |
| 87 | + Some(Command::Sniff { once, .. }) => assert!(once), |
| 88 | + _ => panic!("Expected Sniff command"), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_sniff_with_consume_flag() { |
| 94 | + let cli = Cli::parse_from(["stackdog", "sniff", "--consume"]); |
| 95 | + match cli.command { |
| 96 | + Some(Command::Sniff { consume, .. }) => assert!(consume), |
| 97 | + _ => panic!("Expected Sniff command"), |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_sniff_with_all_options() { |
| 103 | + let cli = Cli::parse_from([ |
| 104 | + "stackdog", "sniff", |
| 105 | + "--once", |
| 106 | + "--consume", |
| 107 | + "--output", "/tmp/logs/", |
| 108 | + "--sources", "/var/log/syslog,/var/log/auth.log", |
| 109 | + "--interval", "60", |
| 110 | + "--ai-provider", "openai", |
| 111 | + ]); |
| 112 | + match cli.command { |
| 113 | + Some(Command::Sniff { once, consume, output, sources, interval, ai_provider }) => { |
| 114 | + assert!(once); |
| 115 | + assert!(consume); |
| 116 | + assert_eq!(output, "/tmp/logs/"); |
| 117 | + assert_eq!(sources.unwrap(), "/var/log/syslog,/var/log/auth.log"); |
| 118 | + assert_eq!(interval, 60); |
| 119 | + assert_eq!(ai_provider.unwrap(), "openai"); |
| 120 | + } |
| 121 | + _ => panic!("Expected Sniff command"), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_sniff_with_candle_provider() { |
| 127 | + let cli = Cli::parse_from(["stackdog", "sniff", "--ai-provider", "candle"]); |
| 128 | + match cli.command { |
| 129 | + Some(Command::Sniff { ai_provider, .. }) => { |
| 130 | + assert_eq!(ai_provider.unwrap(), "candle"); |
| 131 | + } |
| 132 | + _ => panic!("Expected Sniff command"), |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments