syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
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
//! Helmlint result display for terminal output
//!
//! Provides colored, formatted output for Helm chart lint results
//! using Syncable brand styling with box-drawing characters.

use crate::agent::ui::colors::icons;
use crate::agent::ui::response::brand;
use std::io::{self, Write};

/// Box width for consistent display
const BOX_WIDTH: usize = 72;

/// Display helmlint results in a formatted, colored terminal output
pub struct HelmlintDisplay;

impl HelmlintDisplay {
    /// Format and print helmlint results from the JSON output
    pub fn print_result(json_result: &str) {
        if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_result) {
            Self::print_formatted(&parsed);
        } else {
            // Fallback: just print the raw result
            println!("{}", json_result);
        }
    }

    /// Print formatted helmlint output with Syncable brand styling
    fn print_formatted(result: &serde_json::Value) {
        let stdout = io::stdout();
        let mut handle = stdout.lock();

        // Chart path
        let chart = result["chart"].as_str().unwrap_or("helm chart");

        // Header
        let _ = writeln!(handle);
        let _ = writeln!(
            handle,
            "{}{}╭─ {} Helmlint {}{}{}",
            brand::PURPLE,
            brand::BOLD,
            icons::HELM,
            "".repeat(BOX_WIDTH - 15),
            brand::DIM,
            brand::RESET
        );

        // Chart path line
        let _ = writeln!(
            handle,
            "{}{}{}{}{}",
            brand::DIM,
            brand::CYAN,
            chart,
            " ".repeat((BOX_WIDTH - 4 - chart.len()).max(0)),
            brand::RESET
        );

        // Empty line
        let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));

        // Decision context
        if let Some(context) = result["decision_context"].as_str() {
            let context_color = if context.contains("Critical") {
                brand::CORAL
            } else if context.contains("High") || context.contains("high") {
                brand::PEACH
            } else if context.contains("Good") || context.contains("No issues") {
                brand::SUCCESS
            } else {
                brand::PEACH
            };

            // Truncate context if too long
            let display_context = if context.len() > BOX_WIDTH - 6 {
                &context[..BOX_WIDTH - 9]
            } else {
                context
            };

            let _ = writeln!(
                handle,
                "{}{}{}{}{}",
                brand::DIM,
                context_color,
                display_context,
                " ".repeat((BOX_WIDTH - 4 - display_context.len()).max(0)),
                brand::RESET
            );
        }

        // Empty line
        let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));

        // Summary counts
        if let Some(summary) = result.get("summary") {
            let total = summary["total"].as_u64().unwrap_or(0);

            if total == 0 {
                let _ = writeln!(
                    handle,
                    "{}{}{} All checks passed! No issues found.{}{}",
                    brand::DIM,
                    brand::SUCCESS,
                    icons::SUCCESS,
                    " ".repeat(BOX_WIDTH - 42),
                    brand::RESET
                );

                // Files checked
                let files = summary["files_checked"].as_u64().unwrap_or(0);
                let stats = format!("{} files checked", files);
                let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));
                let _ = writeln!(
                    handle,
                    "{}{}{}{}{}",
                    brand::DIM,
                    brand::DIM,
                    stats,
                    " ".repeat((BOX_WIDTH - 4 - stats.len()).max(0)),
                    brand::RESET
                );
            } else {
                // Priority breakdown
                if let Some(by_priority) = summary.get("by_priority") {
                    let critical = by_priority["critical"].as_u64().unwrap_or(0);
                    let high = by_priority["high"].as_u64().unwrap_or(0);
                    let medium = by_priority["medium"].as_u64().unwrap_or(0);
                    let low = by_priority["low"].as_u64().unwrap_or(0);

                    let mut counts = String::new();
                    if critical > 0 {
                        counts.push_str(&format!("{} {} critical  ", icons::CRITICAL, critical));
                    }
                    if high > 0 {
                        counts.push_str(&format!("{} {} high  ", icons::HIGH, high));
                    }
                    if medium > 0 {
                        counts.push_str(&format!("{} {} medium  ", icons::MEDIUM, medium));
                    }
                    if low > 0 {
                        counts.push_str(&format!("{} {} low", icons::LOW, low));
                    }

                    let padding = if counts.len() < BOX_WIDTH - 4 {
                        (BOX_WIDTH - 4 - counts.chars().count()).max(0)
                    } else {
                        0
                    };
                    let _ = writeln!(
                        handle,
                        "{}{}{}{}",
                        brand::DIM,
                        counts,
                        " ".repeat(padding),
                        brand::RESET
                    );
                }
            }
        }

        // Quick fixes section
        if let Some(quick_fixes) = result.get("quick_fixes").and_then(|f| f.as_array())
            && !quick_fixes.is_empty()
        {
            let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));
            let _ = writeln!(
                handle,
                "{}{}{} Quick Fixes:{}{}",
                brand::DIM,
                brand::PURPLE,
                icons::FIX,
                " ".repeat(BOX_WIDTH - 18),
                brand::RESET
            );

            for fix in quick_fixes.iter().take(5) {
                if let Some(fix_str) = fix.as_str() {
                    // Split fix into parts if it contains " - "
                    let (issue, remediation) = if let Some(pos) = fix_str.find(" - ") {
                        (&fix_str[..pos], &fix_str[pos + 3..])
                    } else {
                        (fix_str, "")
                    };

                    let issue_display = if issue.len() > BOX_WIDTH - 10 {
                        format!("{}...", &issue[..BOX_WIDTH - 13])
                    } else {
                        issue.to_string()
                    };

                    let _ = writeln!(
                        handle,
                        "{}{}{}{}{}{}",
                        brand::DIM,
                        brand::CYAN,
                        issue_display,
                        " ".repeat((BOX_WIDTH - 8 - issue_display.len()).max(0)),
                        brand::RESET,
                        brand::RESET
                    );

                    if !remediation.is_empty() {
                        let rem_display = if remediation.len() > BOX_WIDTH - 10 {
                            format!("{}...", &remediation[..BOX_WIDTH - 13])
                        } else {
                            remediation.to_string()
                        };
                        let _ = writeln!(
                            handle,
                            "{}{}{}{}{}",
                            brand::DIM,
                            brand::DIM,
                            rem_display,
                            " ".repeat((BOX_WIDTH - 8 - rem_display.len()).max(0)),
                            brand::RESET
                        );
                    }
                }
            }
        }

        // Critical and High priority issues with details
        Self::print_priority_section(
            &mut handle,
            result,
            "critical",
            "Critical Issues",
            brand::CORAL,
        );
        Self::print_priority_section(&mut handle, result, "high", "High Priority", brand::PEACH);

        // Medium/Low summary
        let medium_count = result["action_plan"]["medium"]
            .as_array()
            .map(|a| a.len())
            .unwrap_or(0);
        let low_count = result["action_plan"]["low"]
            .as_array()
            .map(|a| a.len())
            .unwrap_or(0);
        let other_count = medium_count + low_count;

        if other_count > 0 {
            let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));
            let msg = format!(
                "{} {} priority issue{} (use --verbose to see all)",
                other_count,
                if medium_count > 0 {
                    "medium/low"
                } else {
                    "low"
                },
                if other_count == 1 { "" } else { "s" }
            );
            let _ = writeln!(
                handle,
                "{}{}{}{}{}",
                brand::DIM,
                brand::DIM,
                msg,
                " ".repeat((BOX_WIDTH - 4 - msg.len()).max(0)),
                brand::RESET
            );
        }

        // Footer
        let _ = writeln!(
            handle,
            "{}{}{}",
            brand::DIM,
            "".repeat(BOX_WIDTH - 2),
            brand::RESET
        );
        let _ = writeln!(handle);

        let _ = handle.flush();
    }

    /// Print a section for a priority level
    fn print_priority_section(
        handle: &mut io::StdoutLock,
        result: &serde_json::Value,
        priority: &str,
        title: &str,
        color: &str,
    ) {
        if let Some(issues) = result["action_plan"][priority].as_array() {
            if issues.is_empty() {
                return;
            }

            let _ = writeln!(handle, "{}{}", brand::DIM, " ".repeat(BOX_WIDTH - 1));
            let _ = writeln!(
                handle,
                "{}{}{}:{}{}",
                brand::DIM,
                color,
                title,
                " ".repeat((BOX_WIDTH - 4 - title.len() - 1).max(0)),
                brand::RESET
            );

            for issue in issues.iter().take(5) {
                let code = issue["code"].as_str().unwrap_or("???");
                let file = issue["file"].as_str().unwrap_or("");
                let line = issue["line"].as_u64().unwrap_or(0);
                let message = issue["message"].as_str().unwrap_or("");
                let category = issue["category"].as_str().unwrap_or("");

                // Category badge
                let badge = Self::get_category_badge(category);

                // File and line info
                let file_short = if file.len() > 30 {
                    format!("...{}", &file[file.len() - 27..])
                } else {
                    file.to_string()
                };

                // Issue header line
                let header = format!("{}:{} {} {}", file_short, line, code, badge);
                let header_len = header.chars().count();
                let _ = writeln!(
                    handle,
                    "{}{}{}{}{}",
                    brand::DIM,
                    brand::CYAN,
                    header,
                    " ".repeat((BOX_WIDTH - 6 - header_len).max(0)),
                    brand::RESET
                );

                // Message
                let msg_display = if message.len() > BOX_WIDTH - 8 {
                    format!("{}...", &message[..BOX_WIDTH - 11])
                } else {
                    message.to_string()
                };
                let _ = writeln!(
                    handle,
                    "{}{}{}{}",
                    brand::DIM,
                    msg_display,
                    " ".repeat((BOX_WIDTH - 6 - msg_display.len()).max(0)),
                    brand::RESET
                );

                // Fix recommendation
                if let Some(fix) = issue["fix"].as_str() {
                    let fix_display = if fix.len() > BOX_WIDTH - 12 {
                        format!("{}...", &fix[..BOX_WIDTH - 15])
                    } else {
                        fix.to_string()
                    };
                    let _ = writeln!(
                        handle,
                        "{}{}{}{}{}",
                        brand::DIM,
                        brand::CYAN,
                        fix_display,
                        " ".repeat((BOX_WIDTH - 8 - fix_display.len()).max(0)),
                        brand::RESET
                    );
                }
            }

            if issues.len() > 5 {
                let more_msg = format!("... and {} more", issues.len() - 5);
                let _ = writeln!(
                    handle,
                    "{}{}{}{}{}",
                    brand::DIM,
                    brand::DIM,
                    more_msg,
                    " ".repeat((BOX_WIDTH - 6 - more_msg.len()).max(0)),
                    brand::RESET
                );
            }
        }
    }

    /// Get category badge with color
    fn get_category_badge(category: &str) -> String {
        match category {
            "Security" | "security" => format!("{}[SEC]{}", brand::CORAL, brand::RESET),
            "Structure" | "structure" => format!("{}[STRUCT]{}", brand::DIM, brand::RESET),
            "Values" | "values" => format!("{}[VAL]{}", brand::PEACH, brand::RESET),
            "Template" | "template" => format!("{}[TPL]{}", brand::PEACH, brand::RESET),
            "Best Practice" | "best-practice" => format!("{}[BP]{}", brand::CYAN, brand::RESET),
            _ => String::new(),
        }
    }

    /// Format a compact single-line summary for tool call display
    pub fn format_summary(json_result: &str) -> String {
        if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_result) {
            let success = parsed["success"].as_bool().unwrap_or(false);
            let total = parsed["summary"]["total"].as_u64().unwrap_or(0);

            if success && total == 0 {
                format!(
                    "{}{} {} Helm chart OK - no issues{}",
                    brand::SUCCESS,
                    icons::SUCCESS,
                    icons::HELM,
                    brand::RESET
                )
            } else {
                let critical = parsed["summary"]["by_priority"]["critical"]
                    .as_u64()
                    .unwrap_or(0);
                let high = parsed["summary"]["by_priority"]["high"]
                    .as_u64()
                    .unwrap_or(0);

                if critical > 0 {
                    format!(
                        "{}{} {} {} critical, {} high priority issues{}",
                        brand::CORAL,
                        icons::CRITICAL,
                        icons::HELM,
                        critical,
                        high,
                        brand::RESET
                    )
                } else if high > 0 {
                    format!(
                        "{}{} {} {} high priority issues{}",
                        brand::PEACH,
                        icons::HIGH,
                        icons::HELM,
                        high,
                        brand::RESET
                    )
                } else {
                    format!(
                        "{}{} {} {} issues (medium/low){}",
                        brand::PEACH,
                        icons::MEDIUM,
                        icons::HELM,
                        total,
                        brand::RESET
                    )
                }
            }
        } else {
            format!("{} Helmlint analysis complete", icons::HELM)
        }
    }
}

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

    #[test]
    fn test_format_summary_success() {
        let json = r#"{"success": true, "summary": {"total": 0, "by_priority": {"critical": 0, "high": 0, "medium": 0, "low": 0}}}"#;
        let summary = HelmlintDisplay::format_summary(json);
        assert!(summary.contains("OK"));
    }

    #[test]
    fn test_format_summary_high() {
        let json = r#"{"success": false, "summary": {"total": 3, "by_priority": {"critical": 0, "high": 2, "medium": 1, "low": 0}}}"#;
        let summary = HelmlintDisplay::format_summary(json);
        assert!(summary.contains("high"));
    }

    #[test]
    fn test_category_badge() {
        let badge = HelmlintDisplay::get_category_badge("Template");
        assert!(badge.contains("TPL"));
    }

    #[test]
    fn test_print_result_with_issues() {
        // Test that print doesn't panic with real data
        let json = r#"{
            "chart": "test-chart",
            "success": false,
            "decision_context": "High priority issues found. Fix template syntax.",
            "summary": {
                "total": 3,
                "files_checked": 5,
                "by_priority": {"critical": 0, "high": 2, "medium": 1, "low": 0}
            },
            "action_plan": {
                "critical": [],
                "high": [{
                    "code": "HL3001",
                    "file": "templates/deployment.yaml",
                    "line": 15,
                    "category": "Template",
                    "message": "Unclosed template block",
                    "fix": "Add {{- end }} to close the block"
                }, {
                    "code": "HL1007",
                    "file": "Chart.yaml",
                    "line": 1,
                    "category": "Structure",
                    "message": "Missing maintainers field",
                    "fix": "Add maintainers list with name and email"
                }],
                "medium": [{
                    "code": "HL2003",
                    "file": "values.yaml",
                    "line": 8,
                    "category": "Values",
                    "message": "Unused value defined",
                    "fix": "Remove unused value or reference it in templates"
                }],
                "low": []
            },
            "quick_fixes": ["templates/deployment.yaml:15 HL3001 - Add {{- end }}", "Chart.yaml:1 HL1007 - Add maintainers list"]
        }"#;

        // Just test it doesn't panic
        HelmlintDisplay::print_result(json);
    }

    #[test]
    fn test_print_result_success() {
        let json = r#"{
            "chart": "good-chart",
            "success": true,
            "decision_context": "No issues found.",
            "summary": {
                "total": 0,
                "files_checked": 8,
                "by_priority": {"critical": 0, "high": 0, "medium": 0, "low": 0}
            },
            "action_plan": {"critical": [], "high": [], "medium": [], "low": []}
        }"#;

        // Just test it doesn't panic
        HelmlintDisplay::print_result(json);
    }
}