Skip to main content

cellos_ctl/cmd/
diff.rs

1//! `cellctl diff -f formation.yaml` — show what would change on apply.
2//!
3//! Local YAML vs the current server-side formation, rendered as a unified text
4//! diff over canonical JSON. Per Feynman (Session 16): "kubectl diff is useful,
5//! add it." Same single-API-call rule applies: one GET, no writes.
6
7use std::path::Path;
8
9use owo_colors::OwoColorize;
10use serde_json::Value;
11
12use crate::client::CellosClient;
13use crate::exit::{CtlError, CtlResult};
14use crate::model::Formation;
15
16pub async fn run(client: &CellosClient, path: &Path) -> CtlResult<()> {
17    let yaml = std::fs::read_to_string(path)
18        .map_err(|e| CtlError::usage(format!("read {}: {e}", path.display())))?;
19    let local: Value = serde_yaml::from_str(&yaml)?;
20
21    let local_name = extract_name(&local).ok_or_else(|| {
22        CtlError::validation(format!(
23            "{}: cannot find formation name (`name` or `metadata.name`)",
24            path.display()
25        ))
26    })?;
27
28    let remote: Formation = match client
29        .get_json(&format!("/v1/formations/{}", urlencode(&local_name)))
30        .await
31    {
32        Ok(f) => f,
33        // Only a real HTTP 404 from the server means "the formation does not
34        // exist server-side, so applying would create it". Any other error
35        // (transport failure, 5xx, 4xx other than 404) must propagate so the
36        // operator gets a non-zero exit instead of a false "would create"
37        // plan against a server that was never contacted. See SMOKE-TEST
38        // report Finding #1.
39        Err(e) if e.status == Some(404) => {
40            println!("(formation/{local_name} not found on server — would create)");
41            for line in serde_yaml::to_string(&local).unwrap_or_default().lines() {
42                println!("{}", format!("+ {line}").green());
43            }
44            return Ok(());
45        }
46        Err(e) => return Err(e),
47    };
48
49    let local_pretty = serde_json::to_string_pretty(&local).unwrap_or_default();
50    let remote_pretty = serde_json::to_string_pretty(&remote).unwrap_or_default();
51    print_unified_diff(&remote_pretty, &local_pretty);
52    Ok(())
53}
54
55fn extract_name(v: &Value) -> Option<String> {
56    if let Some(n) = v.get("name").and_then(|x| x.as_str()) {
57        return Some(n.to_string());
58    }
59    v.get("metadata")
60        .and_then(|m| m.get("name"))
61        .and_then(|n| n.as_str())
62        .map(|s| s.to_string())
63}
64
65/// Tiny line-level unified-diff: emit `-` for lines only in `a`, `+` for lines
66/// only in `b`, ` ` otherwise. Not Myers diff — good enough for review,
67/// avoids pulling in a diff crate. Loud about that limitation in the header.
68fn print_unified_diff(a: &str, b: &str) {
69    println!("--- server (current)");
70    println!("+++ local  (proposed)");
71    println!(
72        "# (cellctl diff is a shallow line comparison; for fine-grained review use `diff -u`)"
73    );
74    let a_lines: Vec<&str> = a.lines().collect();
75    let b_lines: Vec<&str> = b.lines().collect();
76    let n = a_lines.len().max(b_lines.len());
77    for i in 0..n {
78        let al = a_lines.get(i).copied();
79        let bl = b_lines.get(i).copied();
80        match (al, bl) {
81            (Some(x), Some(y)) if x == y => println!("  {x}"),
82            (Some(x), Some(y)) => {
83                println!("{}", format!("- {x}").red());
84                println!("{}", format!("+ {y}").green());
85            }
86            (Some(x), None) => println!("{}", format!("- {x}").red()),
87            (None, Some(y)) => println!("{}", format!("+ {y}").green()),
88            (None, None) => {}
89        }
90    }
91}
92
93fn urlencode(s: &str) -> String {
94    percent_encoding::utf8_percent_encode(s, percent_encoding::NON_ALPHANUMERIC).collect()
95}