syncable_cli/analyzer/kubelint/formatter/
json.rs1use crate::analyzer::kubelint::lint::LintResult;
4use serde::Serialize;
5
6pub fn format(result: &LintResult) -> String {
8 let output = JsonOutput::from(result);
9 serde_json::to_string_pretty(&output).unwrap_or_else(|_| "{}".to_string())
10}
11
12#[derive(Serialize)]
13struct JsonOutput {
14 failures: Vec<JsonFailure>,
15 summary: JsonSummary,
16}
17
18#[derive(Serialize)]
19struct JsonFailure {
20 check: String,
21 severity: String,
22 message: String,
23 file_path: String,
24 object_name: String,
25 object_kind: String,
26 object_namespace: Option<String>,
27 line: Option<u32>,
28 remediation: Option<String>,
29}
30
31#[derive(Serialize)]
32struct JsonSummary {
33 objects_analyzed: usize,
34 checks_run: usize,
35 total_failures: usize,
36 passed: bool,
37}
38
39impl From<&LintResult> for JsonOutput {
40 fn from(result: &LintResult) -> Self {
41 Self {
42 failures: result.failures.iter().map(JsonFailure::from).collect(),
43 summary: JsonSummary {
44 objects_analyzed: result.summary.objects_analyzed,
45 checks_run: result.summary.checks_run,
46 total_failures: result.failures.len(),
47 passed: result.summary.passed,
48 },
49 }
50 }
51}
52
53impl From<&crate::analyzer::kubelint::types::CheckFailure> for JsonFailure {
54 fn from(f: &crate::analyzer::kubelint::types::CheckFailure) -> Self {
55 Self {
56 check: f.code.to_string(),
57 severity: f.severity.to_string(),
58 message: f.message.clone(),
59 file_path: f.file_path.display().to_string(),
60 object_name: f.object_name.clone(),
61 object_kind: f.object_kind.clone(),
62 object_namespace: f.object_namespace.clone(),
63 line: f.line,
64 remediation: f.remediation.clone(),
65 }
66 }
67}