Skip to main content

tldr_cli/commands/daemon/
list.rs

1//! `tldr daemon list` — print all live daemon entries from the v0.3.0
2//! multi-daemon registry.
3//!
4//! Supersedes `daemon status`'s implicit single-daemon assumption: when
5//! multiple daemons are running, users invoke `daemon list` to enumerate
6//! them and `daemon status --project <path>` (or `daemon stop --project
7//! <path> | --all`) to operate on a chosen one.
8
9use clap::Args;
10use serde::Serialize;
11
12use crate::output::OutputFormat;
13
14use super::daemon_registry::{live_entries, DaemonRegistryEntry};
15
16/// Arguments for the `daemon list` command.
17///
18/// Output format is controlled by the global `--format` flag (default
19/// `json`). No subcommand-local flags are required; the registry contents
20/// fully describe the output.
21#[derive(Debug, Clone, Args, Default)]
22pub struct DaemonListArgs {}
23
24/// JSON output shape: `{"daemons": [...entries...]}`.
25#[derive(Debug, Serialize)]
26struct DaemonListOutput<'a> {
27    daemons: &'a [DaemonRegistryEntry],
28}
29
30impl DaemonListArgs {
31    /// Run the daemon list command.
32    pub fn run(&self, format: OutputFormat, quiet: bool) -> anyhow::Result<()> {
33        let entries = live_entries();
34
35        if quiet {
36            return Ok(());
37        }
38
39        match format {
40            OutputFormat::Json | OutputFormat::Compact => {
41                let out = DaemonListOutput { daemons: &entries };
42                println!("{}", serde_json::to_string_pretty(&out)?);
43            }
44            OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
45                if entries.is_empty() {
46                    println!("No daemons running");
47                } else {
48                    println!("PROJECT\tPID\tSOCKET\tSTARTED_AT");
49                    for e in &entries {
50                        println!(
51                            "{}\t{}\t{}\t{}",
52                            e.project.display(),
53                            e.pid,
54                            e.socket.display(),
55                            e.started_at
56                        );
57                    }
58                }
59            }
60        }
61        Ok(())
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn list_args_default_constructs() {
71        let _args = DaemonListArgs::default();
72    }
73}