diff options
| author | diogo464 <[email protected]> | 2025-06-19 09:08:38 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-06-19 09:08:38 +0100 |
| commit | 413b3214f2a1a5714fe872220c46ff210178ab76 (patch) | |
| tree | ac2716840b33a79be26318f6e3f177b4b61e93c7 /src/main.rs | |
| parent | b7ff8b4b76236d7ceb1abe87d5c1c4ab2cad61bf (diff) | |
Add quiet flag to list command for machine-readable output
- 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 <[email protected]>
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 48 |
1 files changed, 34 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 { | |||
| 35 | Cat(CatArgs), | 35 | Cat(CatArgs), |
| 36 | 36 | ||
| 37 | /// List all running daemon processes | 37 | /// List all running daemon processes |
| 38 | List, | 38 | List(ListArgs), |
| 39 | 39 | ||
| 40 | /// Check status of a daemon process | 40 | /// Check status of a daemon process |
| 41 | Status(StatusArgs), | 41 | Status(StatusArgs), |
| @@ -96,6 +96,13 @@ struct CatArgs { | |||
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | #[derive(Args)] | 98 | #[derive(Args)] |
| 99 | struct ListArgs { | ||
| 100 | /// Quiet mode - output only process data without headers | ||
| 101 | #[arg(short, long)] | ||
| 102 | quiet: bool, | ||
| 103 | } | ||
| 104 | |||
| 105 | #[derive(Args)] | ||
| 99 | struct StatusArgs { | 106 | struct StatusArgs { |
| 100 | /// Process identifier | 107 | /// Process identifier |
| 101 | #[arg(long)] | 108 | #[arg(long)] |
| @@ -136,8 +143,8 @@ fn run_command(command: Commands) -> Result<()> { | |||
| 136 | let show_stderr = !args.stdout || args.stderr; | 143 | let show_stderr = !args.stdout || args.stderr; |
| 137 | cat_logs(&args.id, show_stdout, show_stderr) | 144 | cat_logs(&args.id, show_stdout, show_stderr) |
| 138 | } | 145 | } |
| 139 | Commands::List => { | 146 | Commands::List(args) => { |
| 140 | list_daemons() | 147 | list_daemons(args.quiet) |
| 141 | } | 148 | } |
| 142 | Commands::Status(args) => { | 149 | Commands::Status(args) => { |
| 143 | status_daemon(&args.id) | 150 | status_daemon(&args.id) |
| @@ -501,9 +508,11 @@ fn handle_file_change( | |||
| 501 | Ok(()) | 508 | Ok(()) |
| 502 | } | 509 | } |
| 503 | 510 | ||
| 504 | fn list_daemons() -> Result<()> { | 511 | fn list_daemons(quiet: bool) -> Result<()> { |
| 505 | println!("{:<20} {:<8} {:<10} {}", "ID", "PID", "STATUS", "COMMAND"); | 512 | if !quiet { |
| 506 | println!("{}", "-".repeat(50)); | 513 | println!("{:<20} {:<8} {:<10} {}", "ID", "PID", "STATUS", "COMMAND"); |
| 514 | println!("{}", "-".repeat(50)); | ||
| 515 | } | ||
| 507 | 516 | ||
| 508 | let mut found_any = false; | 517 | let mut found_any = false; |
| 509 | 518 | ||
| @@ -529,19 +538,30 @@ fn list_daemons() -> Result<()> { | |||
| 529 | "DEAD" | 538 | "DEAD" |
| 530 | }; | 539 | }; |
| 531 | 540 | ||
| 532 | // Try to read command from a hypothetical command file | 541 | if quiet { |
| 533 | // For now, we'll just show "N/A" since we don't store the command | 542 | println!("{}:{}:{}", id, pid, status); |
| 534 | let command = "N/A"; | 543 | } else { |
| 535 | 544 | // Try to read command from a hypothetical command file | |
| 536 | println!("{:<20} {:<8} {:<10} {}", id, pid, status, command); | 545 | // For now, we'll just show "N/A" since we don't store the command |
| 546 | let command = "N/A"; | ||
| 547 | println!("{:<20} {:<8} {:<10} {}", id, pid, status, command); | ||
| 548 | } | ||
| 537 | } | 549 | } |
| 538 | Err(_) => { | 550 | Err(_) => { |
| 539 | println!("{:<20} {:<8} {:<10} {}", id, "INVALID", "ERROR", "Invalid PID file"); | 551 | if quiet { |
| 552 | println!("{}:INVALID:ERROR", id); | ||
| 553 | } else { | ||
| 554 | println!("{:<20} {:<8} {:<10} {}", id, "INVALID", "ERROR", "Invalid PID file"); | ||
| 555 | } | ||
| 540 | } | 556 | } |
| 541 | } | 557 | } |
| 542 | } | 558 | } |
| 543 | Err(e) => { | 559 | Err(e) => { |
| 544 | println!("{:<20} {:<8} {:<10} {}", id, "ERROR", "ERROR", format!("Cannot read: {}", e)); | 560 | if quiet { |
| 561 | println!("{}:ERROR:ERROR", id); | ||
| 562 | } else { | ||
| 563 | println!("{:<20} {:<8} {:<10} {}", id, "ERROR", "ERROR", format!("Cannot read: {}", e)); | ||
| 564 | } | ||
| 545 | } | 565 | } |
| 546 | } | 566 | } |
| 547 | } | 567 | } |
| @@ -551,7 +571,7 @@ fn list_daemons() -> Result<()> { | |||
| 551 | } | 571 | } |
| 552 | } | 572 | } |
| 553 | 573 | ||
| 554 | if !found_any { | 574 | if !found_any && !quiet { |
| 555 | println!("No daemon processes found."); | 575 | println!("No daemon processes found."); |
| 556 | } | 576 | } |
| 557 | 577 | ||
