1use 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 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
65fn 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}