From 413b3214f2a1a5714fe872220c46ff210178ab76 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Thu, 19 Jun 2025 09:08:38 +0100 Subject: Add quiet flag to list command for machine-readable output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add ListArgs struct with quiet boolean flag - Convert List command from unit variant to struct variant - In quiet mode: no headers, colon-separated format (id:pid:status) - In normal mode: preserve existing table format - Handle quiet mode in all output scenarios (normal, error cases) - Add comprehensive test for quiet mode functionality Example usage: demon list # Normal table output demon list -q # Quiet: process1:123:RUNNING 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 48 ++++++++++++++++++++++++++++++++++-------------- tests/cli.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 12a08b6..8e2efc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ enum Commands { Cat(CatArgs), /// List all running daemon processes - List, + List(ListArgs), /// Check status of a daemon process Status(StatusArgs), @@ -95,6 +95,13 @@ struct CatArgs { stderr: bool, } +#[derive(Args)] +struct ListArgs { + /// Quiet mode - output only process data without headers + #[arg(short, long)] + quiet: bool, +} + #[derive(Args)] struct StatusArgs { /// Process identifier @@ -136,8 +143,8 @@ fn run_command(command: Commands) -> Result<()> { let show_stderr = !args.stdout || args.stderr; cat_logs(&args.id, show_stdout, show_stderr) } - Commands::List => { - list_daemons() + Commands::List(args) => { + list_daemons(args.quiet) } Commands::Status(args) => { status_daemon(&args.id) @@ -501,9 +508,11 @@ fn handle_file_change( Ok(()) } -fn list_daemons() -> Result<()> { - println!("{:<20} {:<8} {:<10} {}", "ID", "PID", "STATUS", "COMMAND"); - println!("{}", "-".repeat(50)); +fn list_daemons(quiet: bool) -> Result<()> { + if !quiet { + println!("{:<20} {:<8} {:<10} {}", "ID", "PID", "STATUS", "COMMAND"); + println!("{}", "-".repeat(50)); + } let mut found_any = false; @@ -529,19 +538,30 @@ fn list_daemons() -> Result<()> { "DEAD" }; - // Try to read command from a hypothetical command file - // For now, we'll just show "N/A" since we don't store the command - let command = "N/A"; - - println!("{:<20} {:<8} {:<10} {}", id, pid, status, command); + if quiet { + println!("{}:{}:{}", id, pid, status); + } else { + // Try to read command from a hypothetical command file + // For now, we'll just show "N/A" since we don't store the command + let command = "N/A"; + println!("{:<20} {:<8} {:<10} {}", id, pid, status, command); + } } Err(_) => { - println!("{:<20} {:<8} {:<10} {}", id, "INVALID", "ERROR", "Invalid PID file"); + if quiet { + println!("{}:INVALID:ERROR", id); + } else { + println!("{:<20} {:<8} {:<10} {}", id, "INVALID", "ERROR", "Invalid PID file"); + } } } } Err(e) => { - println!("{:<20} {:<8} {:<10} {}", id, "ERROR", "ERROR", format!("Cannot read: {}", e)); + if quiet { + println!("{}:ERROR:ERROR", id); + } else { + println!("{:<20} {:<8} {:<10} {}", id, "ERROR", "ERROR", format!("Cannot read: {}", e)); + } } } } @@ -551,7 +571,7 @@ fn list_daemons() -> Result<()> { } } - if !found_any { + if !found_any && !quiet { println!("No daemon processes found."); } diff --git a/tests/cli.rs b/tests/cli.rs index 7cb68b7..05e3efd 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -343,4 +343,37 @@ fn test_invalid_process_id() { .assert() .success() .stdout(predicate::str::contains("invalid PID file")); +} + +#[test] +fn test_list_quiet_mode() { + let temp_dir = TempDir::new().unwrap(); + + // Test quiet mode with no processes + let mut cmd = Command::cargo_bin("demon").unwrap(); + cmd.current_dir(temp_dir.path()) + .args(&["list", "--quiet"]) + .assert() + .success() + .stdout(predicate::str::is_empty()); + + // Create a process + let mut cmd = Command::cargo_bin("demon").unwrap(); + cmd.current_dir(temp_dir.path()) + .args(&["run", "--id", "quiet-test", "echo", "done"]) + .assert() + .success(); + + // Test quiet mode with process - should output colon-separated format + let mut cmd = Command::cargo_bin("demon").unwrap(); + cmd.current_dir(temp_dir.path()) + .args(&["list", "-q"]) + .assert() + .success() + .stdout(predicate::str::contains("quiet-test:")) + .stdout(predicate::str::contains(":DEAD")) + // Should not contain headers + .stdout(predicate::str::contains("ID").not()) + .stdout(predicate::str::contains("PID").not()) + .stdout(predicate::str::contains("STATUS").not()); } \ No newline at end of file -- cgit