flodl_cli/util/
fdl_yml.rs1use std::fs;
13use std::path::Path;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum AddCommandOutcome {
18 Added,
20 AlreadyPresent,
22}
23
24pub fn add_command(
39 path: &Path,
40 name: &str,
41 description: &str,
42) -> Result<AddCommandOutcome, String> {
43 let content = fs::read_to_string(path)
44 .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
45 let (new_content, outcome) = insert_command(&content, name, description)?;
46 if outcome == AddCommandOutcome::Added {
47 fs::write(path, new_content)
48 .map_err(|e| format!("cannot write {}: {e}", path.display()))?;
49 }
50 Ok(outcome)
51}
52
53fn insert_command(
54 content: &str,
55 name: &str,
56 description: &str,
57) -> Result<(String, AddCommandOutcome), String> {
58 if name.is_empty() {
59 return Err("command name cannot be empty".into());
60 }
61
62 let lines: Vec<&str> = content.lines().collect();
63
64 let header_idx = lines
66 .iter()
67 .position(|l| l.trim_end() == "commands:" && !l.starts_with([' ', '\t']));
68
69 let Some(header_idx) = header_idx else {
70 let mut out = content.to_string();
72 if !out.is_empty() && !out.ends_with('\n') {
73 out.push('\n');
74 }
75 if !out.is_empty() && !out.ends_with("\n\n") {
76 out.push('\n');
77 }
78 out.push_str("commands:\n");
79 out.push_str(&render_entry(" ", name, description));
80 return Ok((out, AddCommandOutcome::Added));
81 };
82
83 let block_end = lines[header_idx + 1..]
85 .iter()
86 .position(|l| !l.is_empty() && !l.starts_with([' ', '\t']))
87 .map(|i| header_idx + 1 + i)
88 .unwrap_or(lines.len());
89
90 let child_indent = lines[header_idx + 1..block_end]
93 .iter()
94 .find(|l| !l.trim().is_empty())
95 .map(|l| {
96 let n = l.chars().take_while(|c| *c == ' ').count();
97 " ".repeat(n)
98 })
99 .unwrap_or_else(|| " ".to_string());
100
101 let key_token = format!("{name}:");
103 for line in &lines[header_idx + 1..block_end] {
104 if !line.starts_with(&child_indent) {
105 continue;
106 }
107 let after_indent = &line[child_indent.len()..];
108 if after_indent.starts_with(' ') {
111 continue;
112 }
113 let trimmed = after_indent.trim_start();
114 if trimmed == key_token
115 || trimmed.starts_with(&format!("{key_token} "))
116 || trimmed.starts_with(&format!("{name} :"))
117 {
118 return Ok((content.to_string(), AddCommandOutcome::AlreadyPresent));
119 }
120 }
121
122 let mut insert_at = header_idx + 1;
124 for (offset, line) in lines[header_idx + 1..block_end].iter().enumerate() {
125 if !line.trim().is_empty() {
126 insert_at = header_idx + 1 + offset + 1;
127 }
128 }
129
130 let entry = render_entry(&child_indent, name, description);
131
132 let mut out = lines[..insert_at].join("\n");
133 if !out.is_empty() {
134 out.push('\n');
135 }
136 let prev_blank = insert_at == header_idx + 1
140 || lines.get(insert_at - 1).is_some_and(|l| l.trim().is_empty());
141 if !prev_blank {
142 out.push('\n');
143 }
144 out.push_str(&entry);
145 if insert_at < lines.len() {
146 out.push_str(&lines[insert_at..].join("\n"));
147 if content.ends_with('\n') {
148 out.push('\n');
149 }
150 }
151 Ok((out, AddCommandOutcome::Added))
152}
153
154fn render_entry(child_indent: &str, name: &str, description: &str) -> String {
155 let mut out = format!("{child_indent}{name}:\n");
156 if !description.is_empty() {
157 out.push_str(&format!("{child_indent}{child_indent}description: {description}\n"));
158 }
159 out
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn appends_to_existing_commands_block() {
168 let input = "\
169description: my project
170
171commands:
172 build:
173 run: cargo build
174 docker: dev
175";
176 let (out, outcome) = insert_command(input, "flodl-hf", "HF integration").unwrap();
177 assert_eq!(outcome, AddCommandOutcome::Added);
178 assert!(out.contains("build:"), "preserves existing: {out}");
179 assert!(out.contains("flodl-hf:"), "appends: {out}");
180 assert!(out.contains("description: HF integration"));
181 let build = out.find("build:").unwrap();
183 let new = out.find("flodl-hf:").unwrap();
184 assert!(new > build);
185 }
186
187 #[test]
188 fn already_present_is_noop() {
189 let input = "\
190commands:
191 flodl-hf:
192 description: existing entry
193 build:
194 run: cargo build
195";
196 let (out, outcome) = insert_command(input, "flodl-hf", "new desc").unwrap();
197 assert_eq!(outcome, AddCommandOutcome::AlreadyPresent);
198 assert_eq!(out, input);
199 }
200
201 #[test]
202 fn missing_commands_block_appends_at_eof() {
203 let input = "description: my project\n";
204 let (out, outcome) = insert_command(input, "flodl-hf", "HF").unwrap();
205 assert_eq!(outcome, AddCommandOutcome::Added);
206 assert!(out.contains("commands:"));
207 assert!(out.contains(" flodl-hf:"));
208 assert!(out.contains(" description: HF"));
209 }
210
211 #[test]
212 fn empty_commands_block_inserts_first_child() {
213 let input = "commands:\n";
214 let (out, outcome) = insert_command(input, "flodl-hf", "HF").unwrap();
215 assert_eq!(outcome, AddCommandOutcome::Added);
216 assert!(out.contains(" flodl-hf:"));
218 assert!(out.contains(" description: HF"));
219 }
220
221 #[test]
222 fn detects_existing_indent_and_matches_it() {
223 let input = "\
225commands:
226 build:
227 run: cargo build
228";
229 let (out, _) = insert_command(input, "flodl-hf", "HF").unwrap();
230 assert!(out.contains(" flodl-hf:"));
231 assert!(out.contains(" description: HF"));
232 }
233
234 #[test]
235 fn empty_description_omits_subfield() {
236 let input = "commands:\n build:\n run: cargo build\n";
237 let (out, _) = insert_command(input, "flodl-hf", "").unwrap();
238 assert!(out.contains(" flodl-hf:"));
239 assert!(!out.contains("description: \n"), "no empty description: {out}");
240 }
241
242 #[test]
243 fn neighbouring_command_name_does_not_false_positive() {
244 let input = "commands:\n flodl-hf:\n description: existing\n";
247 let (out, outcome) = insert_command(input, "flodl", "new").unwrap();
248 assert_eq!(outcome, AddCommandOutcome::Added);
249 assert!(out.contains("flodl-hf:"));
250 assert!(out.contains("flodl:"));
251 }
252
253 #[test]
254 fn preserves_trailing_content_after_block() {
255 let input = "\
258commands:
259 build:
260 run: cargo build
261
262other_top_level: foo
263";
264 let (out, _) = insert_command(input, "flodl-hf", "HF").unwrap();
265 assert!(out.contains("other_top_level: foo"), "trailing key preserved: {out}");
266 let new = out.find("flodl-hf:").unwrap();
268 let other = out.find("other_top_level:").unwrap();
269 assert!(new < other);
270 }
271
272 #[test]
273 fn empty_name_errors() {
274 let err = insert_command("commands:\n", "", "x").unwrap_err();
275 assert!(err.contains("name cannot be empty"));
276 }
277}