sylphx-cli 0.2.10

Sylphx Platform CLI — dogfoods the Rust Management SDK
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Human output design system for the operator CLI.
//!
//! Goals:
//! - Semantic color (pass/warn/fail/info/muted) with `NO_COLOR` / `--color` respect
//! - Aligned tables for list commands
//! - Actionable error mapping (never dump secrets)
//! - Dual-mode: human for operators, JSON for agents (`--json`)

use std::env;
use std::io::{self, IsTerminal, Write};
use std::sync::OnceLock;

// stdin IsTerminal is used by confirm_or_yes

use serde::Serialize;
use serde_json::Value;
use sylphx_sdk_core::SdkError;

// ── Color / capability ──────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorMode {
    Auto,
    Always,
    Never,
}

static COLOR_MODE: OnceLock<ColorMode> = OnceLock::new();

/// Install global color policy once at process start.
pub fn init_color(mode: ColorMode) {
    let _ = COLOR_MODE.set(mode);
}

fn color_enabled() -> bool {
    match *COLOR_MODE.get().unwrap_or(&ColorMode::Auto) {
        ColorMode::Always => true,
        ColorMode::Never => false,
        ColorMode::Auto => {
            if env::var_os("NO_COLOR").is_some() {
                return false;
            }
            if matches!(
                env::var("CLICOLOR").ok().as_deref(),
                Some("0")
            ) {
                return false;
            }
            if matches!(
                env::var("CLICOLOR_FORCE").ok().as_deref(),
                Some(v) if v != "0"
            ) {
                return true;
            }
            io::stdout().is_terminal()
        }
    }
}

const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
const DIM: &str = "\x1b[2m";
const RED: &str = "\x1b[31m";
const GREEN: &str = "\x1b[32m";
const YELLOW: &str = "\x1b[33m";
const BLUE: &str = "\x1b[34m";
const MAGENTA: &str = "\x1b[35m";
const CYAN: &str = "\x1b[36m";
const BRIGHT_BLACK: &str = "\x1b[90m";

fn paint(code: &str, text: &str) -> String {
    if color_enabled() {
        format!("{code}{text}{RESET}")
    } else {
        text.to_string()
    }
}

pub fn bold(text: &str) -> String {
    paint(BOLD, text)
}
pub fn dim(text: &str) -> String {
    paint(DIM, text)
}
pub fn green(text: &str) -> String {
    paint(GREEN, text)
}
pub fn yellow(text: &str) -> String {
    paint(YELLOW, text)
}
pub fn red(text: &str) -> String {
    paint(RED, text)
}
#[allow(dead_code)]
pub fn blue(text: &str) -> String {
    paint(BLUE, text)
}
pub fn cyan(text: &str) -> String {
    paint(CYAN, text)
}
#[allow(dead_code)]
pub fn magenta(text: &str) -> String {
    paint(MAGENTA, text)
}
pub fn muted(text: &str) -> String {
    paint(BRIGHT_BLACK, text)
}

// ── Semantic marks ──────────────────────────────────────────────────────────

pub fn mark_pass() -> String {
    green("")
}
pub fn mark_warn() -> String {
    yellow("!")
}
pub fn mark_fail() -> String {
    red("")
}
pub fn mark_info() -> String {
    cyan("")
}

pub fn ok_line(msg: &str) {
    println!("{} {msg}", mark_pass());
}
#[allow(dead_code)]
pub fn warn_line(msg: &str) {
    println!("{} {msg}", mark_warn());
}
#[allow(dead_code)]
pub fn fail_line(msg: &str) {
    eprintln!("{} {msg}", mark_fail());
}
#[allow(dead_code)]
pub fn info_line(msg: &str) {
    println!("{} {msg}", mark_info());
}

pub fn section(title: &str) {
    println!("{}", bold(title));
}

pub fn kv(key: &str, value: impl AsRef<str>) {
    let k = format!("{key:<12}");
    println!("  {} {}", muted(&k), value.as_ref());
}

/// Color a deploy/runtime status token.
pub fn status_badge(status: &str) -> String {
    let s = status.trim();
    let lower = s.to_ascii_lowercase();
    match lower.as_str() {
        "ok" | "healthy" | "up" | "running" | "ready" | "active" | "succeeded"
        | "success" | "live" | "complete" | "completed" | "enabled" => green(s),
        "warn" | "warning" | "degraded" | "pending" | "deploying" | "building"
        | "progressing" | "waiting" | "queued" | "in_progress" | "rolling" => yellow(s),
        "fail" | "failed" | "error" | "unhealthy" | "down" | "crashed" | "cancelled"
        | "canceled" | "timeout" | "disabled" | "deleted" => red(s),
        "unknown" | "idle" | "none" | "-" => muted(s),
        _ => cyan(s),
    }
}

// ── Dual output ─────────────────────────────────────────────────────────────

pub fn print_out<T: Serialize>(json: bool, value: &T, human: impl FnOnce()) {
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(value).unwrap_or_else(|_| "{}".into())
        );
    } else {
        human();
    }
}

#[allow(dead_code)]
pub fn emit_json_or<T: Serialize>(
    json: bool,
    value: &T,
    human: impl FnOnce(),
) -> anyhow::Result<()> {
    print_out(json, value, human);
    Ok(())
}

// ── Tables ──────────────────────────────────────────────────────────────────

/// Print an aligned column table. Empty rows print a muted empty message.
pub fn print_table(headers: &[&str], rows: &[Vec<String>]) {
    if rows.is_empty() {
        println!("{}", muted("(none)"));
        return;
    }
    let cols = headers.len();
    let mut widths: Vec<usize> = headers.iter().map(|h| visible_width(h)).collect();
    for row in rows {
        for (i, cell) in row.iter().enumerate().take(cols) {
            widths[i] = widths[i].max(visible_width(cell));
        }
    }

    // Header
    let mut header_line = String::new();
    for (i, h) in headers.iter().enumerate() {
        if i > 0 {
            header_line.push_str("  ");
        }
        header_line.push_str(&pad_visible(h, widths[i]));
    }
    println!("{}", bold(&header_line));

    // Separator
    let mut sep = String::new();
    for (i, w) in widths.iter().enumerate() {
        if i > 0 {
            sep.push_str("  ");
        }
        sep.push_str(&"".repeat(*w));
    }
    println!("{}", muted(&sep));

    // Rows
    for row in rows {
        let mut line = String::new();
        for i in 0..cols {
            if i > 0 {
                line.push_str("  ");
            }
            let cell = row.get(i).map(|s| s.as_str()).unwrap_or("");
            line.push_str(&pad_visible(cell, widths[i]));
        }
        println!("{line}");
    }
}

/// Simple TSV-like key columns for narrow terminals: prefer table when TTY.
pub fn print_id_slug_name(rows: &[(String, String, String)]) {
    let table_rows: Vec<Vec<String>> = rows
        .iter()
        .map(|(id, slug, name)| vec![id.clone(), slug.clone(), name.clone()])
        .collect();
    print_table(&["ID", "SLUG", "NAME"], &table_rows);
}

// ── JSON humanization ───────────────────────────────────────────────────────

/// Render a common Management list/object envelope for humans.
/// Handles `{ data: [...] }`, plain arrays, and single objects.
pub fn print_json_human(value: &Value) {
    match value {
        Value::Array(arr) => print_value_rows(arr),
        Value::Object(map) => {
            if let Some(Value::Array(arr)) = map.get("data") {
                print_value_rows(arr);
                if let Some(n) = map.get("count").and_then(|v| v.as_u64()) {
                    println!("{}", muted(&format!("{n} total")));
                } else if !arr.is_empty() {
                    println!("{}", muted(&format!("{} row(s)", arr.len())));
                }
            } else {
                // Single resource object — key/value block
                print_object_kv(value);
            }
        }
        other => println!("{}", serde_json::to_string_pretty(other).unwrap_or_default()),
    }
}

fn print_value_rows(arr: &[Value]) {
    if arr.is_empty() {
        println!("{}", muted("(none)"));
        return;
    }
    // Prefer common identity columns when present.
    let prefer = [
        "id",
        "name",
        "slug",
        "status",
        "kind",
        "key",
        "domain",
        "enabled",
        "envType",
        "env_type",
        "region",
    ];
    let mut headers: Vec<String> = Vec::new();
    for key in prefer {
        if arr.iter().any(|v| v.get(key).is_some()) {
            headers.push(key.to_string());
        }
    }
    // Cap columns; if none matched, take first object keys.
    if headers.is_empty() {
        if let Some(Value::Object(m)) = arr.first() {
            headers = m.keys().take(5).cloned().collect();
        }
    }
    if headers.is_empty() {
        for v in arr {
            println!("{}", serde_json::to_string(v).unwrap_or_default());
        }
        return;
    }

    let rows: Vec<Vec<String>> = arr
        .iter()
        .map(|item| {
            headers
                .iter()
                .map(|h| {
                    let raw = item.get(h).map(format_cell).unwrap_or_else(|| "-".into());
                    if h == "status" || h == "enabled" {
                        // Keep raw for width; color later via print path
                        raw
                    } else {
                        raw
                    }
                })
                .collect()
        })
        .collect();

    // Color status column after width calc would break alignment — color cells in place
    // by printing manually with status_badge for status/enabled.
    let cols = headers.len();
    let mut widths: Vec<usize> = headers.iter().map(|h| visible_width(h)).collect();
    for row in &rows {
        for (i, cell) in row.iter().enumerate().take(cols) {
            widths[i] = widths[i].max(visible_width(cell));
        }
    }

    let mut header_line = String::new();
    for (i, h) in headers.iter().enumerate() {
        if i > 0 {
            header_line.push_str("  ");
        }
        header_line.push_str(&pad_visible(&h.to_ascii_uppercase(), widths[i]));
    }
    println!("{}", bold(&header_line));
    let mut sep = String::new();
    for (i, w) in widths.iter().enumerate() {
        if i > 0 {
            sep.push_str("  ");
        }
        sep.push_str(&"".repeat(*w));
    }
    println!("{}", muted(&sep));

    for (row_idx, row) in rows.iter().enumerate() {
        let mut line = String::new();
        for i in 0..cols {
            if i > 0 {
                line.push_str("  ");
            }
            let cell = row.get(i).map(|s| s.as_str()).unwrap_or("-");
            let h = headers[i].as_str();
            let display = if h == "status" {
                status_badge(cell)
            } else if h == "enabled" {
                match cell {
                    "true" => green("true"),
                    "false" => muted("false"),
                    other => other.to_string(),
                }
            } else if h == "id" {
                muted(cell)
            } else {
                cell.to_string()
            };
            // Pad using uncolored width
            let pad = widths[i].saturating_sub(visible_width(cell));
            line.push_str(&display);
            line.push_str(&" ".repeat(pad));
            let _ = row_idx;
        }
        println!("{line}");
    }
}

fn print_object_kv(value: &Value) {
    if let Value::Object(map) = value {
        let priority = [
            "status",
            "id",
            "projectId",
            "project_id",
            "name",
            "slug",
            "serviceCount",
            "environmentCount",
            "deploymentId",
            "lastDeployedAt",
        ];
        let mut keys: Vec<&String> = Vec::new();
        for p in priority {
            if let Some(k) = map.keys().find(|k| k.as_str() == p) {
                keys.push(k);
            }
        }
        let mut rest: Vec<&String> = map
            .keys()
            .filter(|k| !keys.iter().any(|x| *x == *k))
            .collect();
        rest.sort();
        keys.extend(rest);
        for k in keys {
            let v = format_cell(&map[k]);
            let key = format!("{k:<22}");
            let display = if k == "status" {
                status_badge(&v)
            } else {
                v
            };
            println!("  {} {}", muted(&key), display);
        }
    } else {
        println!("{}", serde_json::to_string_pretty(value).unwrap_or_default());
    }
}

fn format_cell(v: &Value) -> String {
    match v {
        Value::Null => "-".into(),
        Value::Bool(b) => b.to_string(),
        Value::Number(n) => n.to_string(),
        Value::String(s) => {
            if s.chars().count() > 48 {
                let mut out: String = s.chars().take(45).collect();
                out.push('');
                out
            } else {
                s.clone()
            }
        }
        Value::Array(a) => format!("[{}]", a.len()),
        Value::Object(_) => "{…}".into(),
    }
}

// ── Doctor rows ─────────────────────────────────────────────────────────────

pub fn print_doctor_rows(
    rows: &[(/*status*/ &str, /*title*/ &str, /*detail*/ &str, /*fix*/ Option<&str>)],
) {
    section("sylphx doctor");
    for (status, title, detail, fix) in rows {
        let mark = match *status {
            "pass" => mark_pass(),
            "warn" => mark_warn(),
            "fail" => mark_fail(),
            _ => mark_info(),
        };
        let detail_short = truncate_detail(detail, 120);
        println!("  {mark} {}{}", bold(title), detail_short);
        if let Some(f) = fix {
            println!("      {} {}", muted("fix:"), cyan(f));
        }
    }
}

fn truncate_detail(s: &str, max: usize) -> String {
    // Prefer short form for decode-lag noise: drop giant JSON body.
    if let Some(idx) = s.find("; body=") {
        let head = &s[..idx];
        return truncate_chars(head, max);
    }
    if let Some(idx) = s.find(" body={") {
        let head = &s[..idx];
        return truncate_chars(head, max);
    }
    truncate_chars(s, max)
}

fn truncate_chars(s: &str, max: usize) -> String {
    let count = s.chars().count();
    if count <= max {
        return s.to_string();
    }
    let mut out: String = s.chars().take(max.saturating_sub(1)).collect();
    out.push('');
    out
}

// ── Width helpers (ANSI-aware enough for our own codes) ─────────────────────

fn strip_ansi(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\u{1b}' {
            if chars.peek() == Some(&'[') {
                chars.next();
                for x in chars.by_ref() {
                    if x.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
        } else {
            out.push(c);
        }
    }
    out
}

fn visible_width(s: &str) -> usize {
    strip_ansi(s).chars().count()
}

fn pad_visible(s: &str, width: usize) -> String {
    let w = visible_width(s);
    if w >= width {
        s.to_string()
    } else {
        format!("{s}{}", " ".repeat(width - w))
    }
}

// ── Error mapping ───────────────────────────────────────────────────────────

/// Map SDK errors into operator-actionable anyhow errors.
pub fn map_sdk_err(err: SdkError) -> anyhow::Error {
    match &err {
        SdkError::Api {
            status: 404,
            code,
            message,
        } if message.contains("/whoami") || message.contains("\"path\":\"/whoami\"") => {
            anyhow::anyhow!(
                "api 404: {code}: Management whoami route not found.\n\
                 \n\
                 Likely cause: API base URL is missing `/v1`\n\
                   wrong:  https://api.sylphx.com/whoami\n\
                   right:  https://api.sylphx.com/v1/whoami\n\
                 \n\
                 Fix:\n\
                   • sylphx doctor\n\
                   • sylphx login --token \"$SYLPHX_TOKEN\" --api-url https://api.sylphx.com/v1\n\
                   • or: export SYLPHX_API_URL=https://api.sylphx.com/v1\n\
                 \n\
                 Raw: {message}"
            )
        }
        SdkError::Api {
            status: 403,
            code,
            message,
        } if message.contains("user_context_required") => anyhow::anyhow!(
            "api 403: {code}: this endpoint needs a user-scoped credential.\n\
             \n\
             You are likely using a service token (`svc_*`).\n\
               • whoami / user profile → use `sylphx login` (device flow)\n\
               • deploy / projects / automation → `svc_*` is correct\n\
             \n\
             Raw: {message}"
        ),
        SdkError::Api {
            status: 401,
            code,
            message,
        } => anyhow::anyhow!(
            "api 401: {code}: not authenticated.\n\
             \n\
             Fix:\n\
               • sylphx login\n\
               • sylphx login --token svc_…\n\
               • export SYLPHX_TOKEN=…\n\
             \n\
             Then: sylphx doctor\n\
             Raw: {message}"
        ),
        SdkError::Api {
            status: 404,
            code,
            message,
        } => anyhow::anyhow!(
            "api 404: {code}: resource not found.\n\
             \n\
             Check:\n\
               • project/org id spelling\n\
               • preferred org (`sylphx context show` / `--org-id`)\n\
               • sylphx doctor\n\
             \n\
             Raw: {message}"
        ),
        SdkError::Decode(msg) => anyhow::anyhow!(
            "wire decode lag: Management API returned fields the local SDK contract does not know yet.\n\
             \n\
             This is usually additive API evolution ahead of a CLI/SDK release.\n\
             Fix:\n\
               • sylphx update\n\
               • or use: sylphx api get <path> --json  (raw envelope)\n\
             \n\
             Detail: {msg}"
        ),
        other => anyhow::anyhow!("{other}"),
    }
}

/// Preserve an actionable, secret-free API failure when login verification
/// rejects a freshly issued credential. `anyhow::Context` is not sufficient
/// here because the CLI intentionally prints only the outer error line.
pub fn login_verification_err(err: SdkError) -> anyhow::Error {
    anyhow::anyhow!(
        "login verification failed — credentials were NOT saved: {}",
        map_sdk_err(err)
    )
}

/// Confirm a destructive action unless `--yes` / non-interactive JSON mode.
///
/// - `--yes` always proceeds
/// - `--json` without `--yes` fails closed (agents must opt in)
/// - interactive TTY prompts `y/N`
pub fn confirm_or_yes(yes: bool, json: bool, prompt: &str) -> anyhow::Result<()> {
    if yes {
        return Ok(());
    }
    if json {
        anyhow::bail!(
            "refusing destructive action without --yes (json mode is non-interactive).\n\
             Re-run with --yes after reviewing: {prompt}"
        );
    }
    if !io::stdin().is_terminal() {
        anyhow::bail!(
            "refusing destructive action without --yes (stdin is not a TTY).\n\
             Re-run with --yes after reviewing: {prompt}"
        );
    }
    eprint!("{} {} [y/N] ", mark_warn(), prompt);
    let _ = io::stderr().flush();
    let mut line = String::new();
    io::stdin().read_line(&mut line)?;
    let answer = line.trim().to_ascii_lowercase();
    if answer == "y" || answer == "yes" {
        Ok(())
    } else {
        anyhow::bail!("aborted")
    }
}

/// Pretty-print a top-level CLI error to stderr.
pub fn print_error(err: &anyhow::Error) {
    let msg = err.to_string();
    let first = msg.lines().next().unwrap_or("error");
    eprintln!("{} {}", mark_fail(), bold(first));
    for line in msg.lines().skip(1) {
        if line.is_empty() {
            eprintln!();
        } else {
            eprintln!("  {line}");
        }
    }
    let _ = io::stderr().flush();
}

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

    #[test]
    fn login_verification_error_keeps_sanitized_api_cause() {
        let error = login_verification_err(SdkError::Api {
            status: 401,
            code: "invalid_audience".into(),
            message: "credential audience rejected".into(),
        })
        .to_string();

        assert!(error.contains("credentials were NOT saved"));
        assert!(error.contains("api 401: invalid_audience"));
        assert!(error.contains("credential audience rejected"));
    }

    #[test]
    fn truncate_detail_strips_json_body() {
        let s = "status=ok (typed HealthResponse decode lag: decode: unknown field `draining`; body={\"draining\":false,\"status\":\"ok\"})";
        let t = truncate_detail(s, 200);
        assert!(!t.contains("\"draining\""));
        assert!(t.contains("status=ok"));
    }

    #[test]
    fn format_cell_truncates_long_strings() {
        let long = "x".repeat(80);
        let v = Value::String(long);
        let cell = format_cell(&v);
        assert!(cell.ends_with(''));
        assert!(cell.chars().count() <= 48);
    }

    #[test]
    fn status_badge_known_tokens() {
        // Color may be off in tests; just ensure non-empty.
        assert!(!status_badge("running").is_empty());
        assert!(!status_badge("failed").is_empty());
        assert!(!status_badge("deploying").is_empty());
    }
}