coralogix-cli 0.1.26

Coralogix CLI - the observability backbone for AI agents and engineering teams.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
pub mod api;

use std::sync::Arc;

use anyhow::Result;
use colored::Colorize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;

use crate::config::OutputFormat;
use crate::execution::{fan_out, report_errors_and_collect_successes, ExecutionTarget};
use crate::render;
use api::{Webhook, WebhooksApi};

fn webhook_to_json(webhook: &Webhook, include_profile: bool, profile: &str) -> Value {
    let mut v = json!({
        "id": webhook.id,
        "name": webhook.name,
        "type": webhook.display_type(),
        "url": webhook.url,
        "created_at": webhook.created_at,
    });
    if include_profile {
        if let Value::Object(ref mut m) = v {
            m.insert("profile".to_string(), Value::String(profile.to_string()));
        }
    }
    v
}

/// Extract a webhook ID from a create response, trying the shapes the API
/// has been observed to return: nested `webhook.id`, or a bare top-level
/// `id`. IDs may come back as a JSON string or number. Returns `None` when
/// the response carried no ID so callers can flag that rather than
/// fabricate one.
fn webhook_id_from_response(resp: &Value) -> Option<String> {
    fn at(v: &Value, pointer: &str) -> Option<String> {
        match v.pointer(pointer)? {
            Value::String(s) => Some(s.clone()),
            Value::Number(n) => Some(n.to_string()),
            _ => None,
        }
    }
    at(resp, "/webhook/id").or_else(|| at(resp, "/id"))
}

/// Pull the display name out of a create *request* body. The create response
/// only carries an id, so the name has to come from what was sent. Outgoing
/// webhook payloads nest it under `data` (`{"data": {"name": ...}}`); the
/// root `name` fallback covers hand-written flat payloads.
fn webhook_name_from_request(body: &Value) -> Option<&str> {
    body.pointer("/data/name")
        .or_else(|| body.get("name"))
        .and_then(|v| v.as_str())
}

fn read_from_file(path: &str) -> Result<Value> {
    let raw = if path == "-" {
        eprintln!("{}", "Reading webhook definition from stdin...".dimmed());
        use std::io::Read;
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf)?;
        buf
    } else {
        eprintln!(
            "{}",
            format!("Reading webhook definition from {path}...").dimmed()
        );
        std::fs::read_to_string(path)?
    };
    Ok(serde_json::from_str(&raw)?)
}

pub async fn run_list(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
    eprintln!("{}", "Fetching webhooks...".dimmed());
    let include_profile = targets.len() > 1;

    let per_profile = fan_out(targets, |t| async move {
        let api = WebhooksApi::new(&t.client);
        Ok(api.list_all().await?)
    })
    .await;

    let mut all_json: Vec<Value> = Vec::new();
    let mut all_items: Vec<(String, Webhook)> = Vec::new();
    for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
        // Print the outbound-webhooks page link to stderr once per profile.
        // Skip when there are no webhooks, since there's nothing to view.
        if !resp.deployed.is_empty() {
            crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
                crate::console_url::webhooks_url(b)
            })
            .await;
        }
        for webhook in resp.deployed {
            let webhook_json = webhook_to_json(&webhook, include_profile, &profile);
            all_json.push(webhook_json);
            all_items.push((profile.clone(), webhook));
        }
    }

    match output {
        OutputFormat::Json => render::render_json(&all_json)?,
        OutputFormat::Toon => {
            let toon =
                toon_encode(&all_json).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {
            if all_items.is_empty() {
                render::print_no_results("No webhooks found.");
                return Ok(());
            }
            let rows: Vec<Vec<String>> = all_items
                .iter()
                .map(|(profile, webhook)| {
                    vec![
                        profile.clone(),
                        webhook.id.clone().unwrap_or_default(),
                        webhook.name.clone().unwrap_or_default(),
                        webhook.display_type().to_string(),
                        webhook.url.clone().unwrap_or_default(),
                        webhook.created_at.clone().unwrap_or_default(),
                    ]
                })
                .collect();
            render::render_table(
                &["ID", "Name", "Type", "URL", "Created At"],
                rows,
                include_profile,
            );
        }
    }
    Ok(())
}

pub async fn run_get(
    targets: &[Arc<ExecutionTarget>],
    id: &str,
    output: OutputFormat,
) -> Result<()> {
    eprintln!("{}", format!("Fetching webhook {id}...").dimmed());
    let include_profile = targets.len() > 1;
    let id = id.to_string();

    let per_profile = fan_out(targets, |t| {
        let id = id.clone();
        async move {
            let api = WebhooksApi::new(&t.client);
            Ok(api.get(&id).await?)
        }
    })
    .await;

    let mut all_results: Vec<Value> = Vec::new();
    for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
        if include_profile {
            render::tag_get_result(&mut val, &profile);
        }
        crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
            crate::console_url::webhooks_url(b)
        })
        .await;
        all_results.push(val);
    }

    match output {
        OutputFormat::Json => render::render_json_auto(&all_results)?,
        OutputFormat::Toon => {
            let toon = toon_encode(&all_results)
                .map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {
            render::render_get_text(
                &all_results,
                include_profile,
                "Webhook not found.",
                None::<&dyn Fn(&Value)>,
            )?;
        }
    }
    Ok(())
}

pub async fn run_create(
    targets: &[Arc<ExecutionTarget>],
    from_file: &str,
    output: OutputFormat,
) -> Result<()> {
    let body = read_from_file(from_file)?;
    let name = webhook_name_from_request(&body)
        .unwrap_or("<unnamed>")
        .to_string();
    eprintln!("{}", "Creating webhook...".dimmed());
    let include_profile = targets.len() > 1;

    let per_profile = fan_out(targets, |t| {
        let body = body.clone();
        async move {
            let api = WebhooksApi::new(&t.client);
            Ok(api.create(&body).await?)
        }
    })
    .await;

    let mut all_results: Vec<Value> = Vec::new();
    for (profile, mut resp) in report_errors_and_collect_successes(per_profile)? {
        let created_id = webhook_id_from_response(&resp);
        render::print_created(
            "Created",
            "webhook",
            Some(&name),
            created_id.as_deref(),
            &profile,
        );
        if include_profile {
            // Create output uses the public `profile` key (list/create
            // convention, see render.rs), not the get-only `_profile` hint.
            if let Value::Object(ref mut m) = resp {
                m.insert("profile".to_string(), Value::String(profile.to_string()));
            }
        }
        crate::execution::emit_console_link_for_profile(
            targets,
            &profile,
            crate::console_url::webhooks_url,
        )
        .await;
        all_results.push(resp);
    }

    match output {
        OutputFormat::Json => render::render_json_auto(&all_results)?,
        OutputFormat::Toon => {
            let toon = toon_encode(&all_results)
                .map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {}
    }
    Ok(())
}

pub async fn run_update(
    targets: &[Arc<ExecutionTarget>],
    id: &str,
    from_file: &str,
    output: OutputFormat,
) -> Result<()> {
    let body = read_from_file(from_file)?;
    eprintln!("{}", format!("Updating webhook {id}...").dimmed());
    let id = id.to_string();

    let per_profile = fan_out(targets, |t| {
        let body = body.clone();
        let id = id.clone();
        async move {
            let api = WebhooksApi::new(&t.client);
            Ok(api.update(&id, &body).await?)
        }
    })
    .await;

    let mut all_results: Vec<Value> = Vec::new();
    for (profile, val) in report_errors_and_collect_successes(per_profile)? {
        eprintln!(
            "{}",
            format!("Updated webhook {id} in profile '{profile}'.").green()
        );
        crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
            crate::console_url::webhooks_url(b)
        })
        .await;
        all_results.push(val);
    }

    match output {
        OutputFormat::Json => render::render_json_auto(&all_results)?,
        OutputFormat::Toon => {
            let toon = toon_encode(&all_results)
                .map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {}
    }
    Ok(())
}

pub async fn run_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
    eprintln!("{}", format!("Deleting webhook {id}...").dimmed());
    let id = id.to_string();
    let per_profile = fan_out(targets, |t| {
        let id = id.clone();
        async move {
            let api = WebhooksApi::new(&t.client);
            api.delete(&id).await?;
            Ok(())
        }
    })
    .await;
    for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
        eprintln!(
            "{}",
            format!("Webhook {id} deleted in profile '{profile}'.").green()
        );
        crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
            crate::console_url::webhooks_url(b)
        })
        .await;
    }
    Ok(())
}

pub async fn run_test(
    targets: &[Arc<ExecutionTarget>],
    id: &str,
    output: OutputFormat,
) -> Result<()> {
    eprintln!("{}", format!("Testing webhook {id}...").dimmed());
    let id = id.to_string();

    let per_profile = fan_out(targets, |t| {
        let id = id.clone();
        async move {
            let api = WebhooksApi::new(&t.client);
            Ok(api.test(&id).await?)
        }
    })
    .await;

    let mut all_results: Vec<Value> = Vec::new();
    for (profile, val) in report_errors_and_collect_successes(per_profile)? {
        eprintln!(
            "{}",
            format!("Test completed in profile '{profile}'.").green()
        );
        crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
            crate::console_url::webhooks_url(b)
        })
        .await;
        all_results.push(val);
    }

    match output {
        OutputFormat::Json => render::render_json_auto(&all_results)?,
        OutputFormat::Toon => {
            let toon = toon_encode(&all_results)
                .map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {
            for val in &all_results {
                println!("{}", serde_json::to_string_pretty(val)?);
            }
        }
    }
    Ok(())
}

pub async fn run_types(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
    eprintln!("{}", "Fetching webhook types...".dimmed());
    let include_profile = targets.len() > 1;

    let per_profile = fan_out(targets, |t| async move {
        let api = WebhooksApi::new(&t.client);
        Ok(api.list_types().await?)
    })
    .await;

    let mut all_results: Vec<Value> = Vec::new();
    for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
        if include_profile {
            render::tag_get_result(&mut val, &profile);
        }
        crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
            crate::console_url::webhooks_url(b)
        })
        .await;
        all_results.push(val);
    }

    match output {
        OutputFormat::Json => render::render_json_auto(&all_results)?,
        OutputFormat::Toon => {
            let toon = toon_encode(&all_results)
                .map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
            println!("{toon}");
        }
        OutputFormat::Text => {
            for val in &all_results {
                println!("{}", serde_json::to_string_pretty(val)?);
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn webhook_id_from_response_reads_wrapped_shape() {
        let resp = json!({ "webhook": { "id": "wh-001", "name": "Slack Notify" } });
        assert_eq!(webhook_id_from_response(&resp).as_deref(), Some("wh-001"));
    }

    #[test]
    fn webhook_id_from_response_reads_bare_shape() {
        // What the live API actually returns: the created webhook directly,
        // with no `webhook` envelope.
        let resp = json!({ "id": "wh-002", "name": "Slack Notify" });
        assert_eq!(webhook_id_from_response(&resp).as_deref(), Some("wh-002"));
    }

    #[test]
    fn webhook_id_from_response_reads_numeric_id() {
        let resp = json!({ "webhook": { "id": 7 } });
        assert_eq!(webhook_id_from_response(&resp).as_deref(), Some("7"));
    }

    #[test]
    fn webhook_id_from_response_none_when_id_absent() {
        let resp = json!({});
        assert_eq!(webhook_id_from_response(&resp), None);
    }

    #[test]
    fn webhook_name_from_request_reads_nested_data_name() {
        // The real outgoing-webhook create payload shape.
        let body = json!({ "data": { "name": "Slack Notify", "type": "slack" } });
        assert_eq!(webhook_name_from_request(&body), Some("Slack Notify"));
    }

    #[test]
    fn webhook_name_from_request_falls_back_to_root_name() {
        let body = json!({ "name": "Flat Payload" });
        assert_eq!(webhook_name_from_request(&body), Some("Flat Payload"));
    }

    #[test]
    fn webhook_name_from_request_prefers_data_name_over_root() {
        let body = json!({ "name": "outer", "data": { "name": "inner" } });
        assert_eq!(webhook_name_from_request(&body), Some("inner"));
    }

    #[test]
    fn webhook_name_from_request_none_when_absent() {
        assert_eq!(webhook_name_from_request(&json!({})), None);
        // A non-string name is not a usable display name either.
        assert_eq!(webhook_name_from_request(&json!({ "name": 7 })), None);
    }
}