Skip to main content

flodl_cli/util/
fdl_yml.rs

1//! Minimal text-based fdl.yml editor.
2//!
3//! Same policy as [`super::cargo_toml`]: append-only, format-preserving,
4//! no external yaml-edit crate. Scope is appending a top-level command
5//! entry under `commands:` if the entry isn't already declared.
6//!
7//! By fdl.yml convention, a command with neither `run:` nor `path:` and
8//! no preset fields falls through to a Path command with the default
9//! `./<name>/` location, so the appended entry needs no explicit
10//! `path:` to make `fdl <name> <subcmd>` route into `./<name>/fdl.yml`.
11
12use std::fs;
13use std::path::Path;
14
15/// Result of an [`add_command`] call.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum AddCommandOutcome {
18    /// The command entry was appended.
19    Added,
20    /// `name` was already declared under `commands:`; file untouched.
21    AlreadyPresent,
22}
23
24/// Append a top-level command entry under `commands:` in the fdl.yml at
25/// `path` if the entry isn't already declared.
26///
27/// `description` is written as a `description:` subfield. Pass an empty
28/// string to omit it (the entry then has nothing under it, falling back
29/// to the convention-default `path: ./<name>/`).
30///
31/// Behaviour:
32/// - `commands:` table present, `name` absent → append the entry at end
33///   of the commands block, [`AddCommandOutcome::Added`].
34/// - `commands:` present and `name` already declared → file untouched,
35///   [`AddCommandOutcome::AlreadyPresent`].
36/// - `commands:` absent → append `\ncommands:\n  name:\n    description: ...\n`
37///   at end of file, [`AddCommandOutcome::Added`].
38pub 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    // Find top-level `commands:` (indent 0).
65    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        // No commands: table — append a fresh one at EOF.
71        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    // Block ends at the first line at indent 0 (excluding blanks).
84    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    // Detect child indent from the first non-blank child; default to two
91    // spaces when the block is empty (matches scaffold convention).
92    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    // Already declared?
102    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        // Must be a sibling key (no further leading spaces) and match
109        // `name:` or `name :` exactly.
110        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    // Insert AFTER the last non-blank line in the block.
123    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    // Blank line before the entry when the previous content already
137    // had a non-blank line (visual separator between sibling commands).
138    // Skip when the immediately previous line is already blank.
139    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        // New entry comes after `build:`.
182        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        // Default 2-space indent kicks in.
217        assert!(out.contains("  flodl-hf:"));
218        assert!(out.contains("    description: HF"));
219    }
220
221    #[test]
222    fn detects_existing_indent_and_matches_it() {
223        // Existing block uses 4-space indent — new entry must follow.
224        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        // `flodl-hf` and `flodl` are distinct keys; presence of one must
245        // not block adding the other.
246        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        // commands: is followed by another top-level key — new entry
256        // must not bleed into it.
257        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        // `flodl-hf:` lands BEFORE `other_top_level:` (still inside commands block).
267        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}