Skip to main content

pitchfork_cli/cli/
json_output.rs

1use crate::log_store::LogEntry;
2use serde::Serialize;
3
4#[derive(Serialize)]
5pub struct JsonListEntry {
6    pub id: String,
7    pub namespace: String,
8    pub name: String,
9    pub pid: Option<u32>,
10    pub status: String,
11    /// True for a `oneshot = true` daemon. Present so a consumer can tell a
12    /// task from a service rather than inferring it from `completed`, which a
13    /// task only reaches once it has run.
14    pub oneshot: bool,
15    pub disabled: bool,
16    pub available: bool,
17    /// Deprecated alias of `url`, kept so existing consumers keep working.
18    pub proxy_url: Option<String>,
19    /// The daemon's proxy URL.
20    pub url: Option<String>,
21    /// How the proxy handles TLS for this daemon: `terminate` or `passthrough`.
22    /// Omitted when the daemon is not routed through the proxy.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub proxy_tls: Option<String>,
25    pub error: Option<String>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub active_port: Option<u16>,
28    pub port: Vec<u16>,
29    /// Last status the daemon reported over the Program Status Protocol
30    /// (OSC 7501). Omitted when it has not reported one.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub program_status: Option<crate::program_status::ProgramStatus>,
33}
34
35#[derive(Serialize)]
36pub struct JsonStatusEntry {
37    pub id: String,
38    pub namespace: String,
39    pub name: String,
40    pub pid: Option<u32>,
41    pub status: String,
42    /// True for a `oneshot = true` daemon. See `JsonListEntry::oneshot`.
43    pub oneshot: bool,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub active_port: Option<u16>,
46    pub port: Vec<u16>,
47    /// See `JsonListEntry::program_status`.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub program_status: Option<crate::program_status::ProgramStatus>,
50    /// Deprecated alias of `url`, kept so existing consumers keep working.
51    pub proxy_url: Option<String>,
52    /// The daemon's proxy URL.
53    pub url: Option<String>,
54    /// How the proxy handles TLS for this daemon: `terminate` or `passthrough`.
55    /// Omitted when the daemon is not routed through the proxy.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub proxy_tls: Option<String>,
58    /// The daemon's cron expression. Omitted when it is not scheduled; the
59    /// two fields below are omitted with it.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub cron_schedule: Option<String>,
62    /// When the cron watcher last started this daemon, RFC 3339. Omitted
63    /// until a scheduled run has actually happened -- a registered schedule
64    /// that has not come due yet has no last run.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub cron_last_run: Option<String>,
67    /// The next time the schedule comes due, RFC 3339. A time in the past
68    /// means the window was missed while the supervisor was down and the
69    /// watcher will take it on its next check.
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub cron_next_run: Option<String>,
72}
73
74#[derive(Serialize)]
75pub struct JsonLogEntry {
76    /// SQLite row id, used by the webui for backward pagination (scroll-up
77    /// history loading). Skipped in serialization when 0 to avoid changing
78    /// the CLI --json output shape for non-web callers.
79    #[serde(skip_serializing_if = "is_zero_id")]
80    pub id: i64,
81    pub timestamp: String,
82    pub daemon_id: String,
83    pub message: String,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub level: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub msg: Option<String>,
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub logger: Option<String>,
90    /// Parsed structured fields as a JSON object, or null if the line was
91    /// not structured (plain text).
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub fields: Option<serde_json::Value>,
94}
95
96fn is_zero_id(id: &i64) -> bool {
97    *id == 0
98}
99
100impl From<LogEntry> for JsonLogEntry {
101    fn from(e: LogEntry) -> Self {
102        let fields = e
103            .fields_json
104            .as_deref()
105            .and_then(|s| serde_json::from_str(s).ok());
106        JsonLogEntry {
107            id: e.id,
108            timestamp: e.timestamp.format("%Y-%m-%d %H:%M:%S").to_string(),
109            daemon_id: e.daemon_id,
110            message: console::strip_ansi_codes(&e.message).to_string(),
111            level: e.level,
112            msg: e.msg,
113            logger: e.logger,
114            fields,
115        }
116    }
117}
118
119#[derive(Serialize)]
120pub struct JsonDaemonConfigEntry {
121    pub id: String,
122    /// As written in the config: a string, or an array for the argv form.
123    pub run: crate::pitchfork_toml::RunCommand,
124}
125
126#[derive(Serialize)]
127pub struct JsonProxyStatus {
128    pub enabled: bool,
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub scheme: Option<String>,
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub tld: Option<String>,
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub port: Option<u16>,
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub lan: Option<JsonLanInfo>,
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub tls_cert: Option<String>,
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub trusted: Option<bool>,
141    pub slugs: Vec<JsonSlugEntry>,
142    /// Automatic hostnames, grouped by project and worktree.
143    #[serde(default, skip_serializing_if = "Vec::is_empty")]
144    pub projects: Vec<JsonProxyProject>,
145    /// Labels claimed by more than one project, worktree or daemon. Nothing is
146    /// routed under them until the clash is resolved.
147    #[serde(default, skip_serializing_if = "Vec::is_empty")]
148    pub conflicts: Vec<String>,
149}
150
151#[derive(Serialize)]
152pub struct JsonProxyProject {
153    pub project: String,
154    pub dir: String,
155    /// URL of the project page.
156    pub url: String,
157    pub daemons: Vec<JsonProxyHost>,
158    #[serde(skip_serializing_if = "Vec::is_empty")]
159    pub worktrees: Vec<JsonProxyWorktree>,
160}
161
162#[derive(Serialize)]
163pub struct JsonProxyWorktree {
164    pub worktree: String,
165    pub dir: String,
166    /// URL of the stack page.
167    pub url: String,
168    pub daemons: Vec<JsonProxyHost>,
169}
170
171#[derive(Serialize)]
172pub struct JsonProxyHost {
173    pub daemon: String,
174    /// Hostname without the TLD.
175    pub host: String,
176    pub url: String,
177    pub status: String,
178    #[serde(skip_serializing_if = "Option::is_none")]
179    pub port: Option<u16>,
180}
181
182#[derive(Serialize)]
183pub struct JsonLanInfo {
184    pub enabled: bool,
185    pub ip: String,
186}
187
188#[derive(Serialize)]
189pub struct JsonSlugEntry {
190    pub slug: String,
191    /// Absent when the slug is not routable (see `status`).
192    #[serde(skip_serializing_if = "Option::is_none")]
193    pub url: Option<String>,
194    pub dir: String,
195    pub daemon: String,
196    pub status: String,
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub port: Option<u16>,
199    #[serde(skip_serializing_if = "Option::is_none")]
200    pub public_url: Option<String>,
201}
202
203#[derive(Serialize)]
204pub struct JsonSupervisorStatus {
205    pub status: &'static str,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub web_ui: Option<String>,
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub error: Option<String>,
210}
211
212#[derive(Serialize)]
213pub struct JsonSettingEntry {
214    pub key: String,
215    pub value: String,
216    #[serde(skip_serializing_if = "Option::is_none")]
217    pub default: Option<String>,
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub r#type: Option<&'static str>,
220    #[serde(skip_serializing_if = "Option::is_none")]
221    pub env_var: Option<&'static str>,
222    /// Where the winning value came from, named so a script can act on it:
223    /// `PITCHFORK_LOG`, `/path/to/pitchfork.toml#general.log_level`, `the
224    /// default`. Absent only for a setting nothing set and that has no
225    /// default.
226    #[serde(skip_serializing_if = "Option::is_none")]
227    pub origin: Option<String>,
228}
229
230pub fn print_json<T: Serialize>(value: &T) -> crate::Result<()> {
231    let json = serde_json::to_string_pretty(value)
232        .map_err(|e| miette::miette!("failed to serialize JSON: {e}"))?;
233    println!("{json}");
234    Ok(())
235}