croft 0.1.1

Per-project dev-loop tool: Docker Compose, scripts, hooks, and agents in one config
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! `croft doctor` — validate config and report.

use crate::config::Config;
use crate::container::{Backend, compose::ComposeBackend};
use anyhow::Result;
use std::path::Path;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Severity {
    Ok,
    Warn,
    Fail,
}

impl Severity {
    fn marker(self) -> &'static str {
        match self {
            Severity::Ok => "[ OK ]",
            Severity::Warn => "[WARN]",
            Severity::Fail => "[FAIL]",
        }
    }
}

#[derive(Debug, Clone)]
struct Finding {
    severity: Severity,
    message: String,
}

impl Finding {
    fn ok(message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Ok,
            message: message.into(),
        }
    }
    fn warn(message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Warn,
            message: message.into(),
        }
    }
    fn fail(message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Fail,
            message: message.into(),
        }
    }
}

/// Run all checks and print a report. Returns exit code 0 if no FAILs;
/// 1 otherwise. Warnings do not change the exit code.
pub async fn run(config: &Config, project_root: &Path) -> Result<i32> {
    let mut findings = Vec::new();
    findings.extend(check_backend(config).await);
    findings.extend(check_env_files(config, project_root));
    findings.extend(check_dependency_graph(config));
    findings.extend(check_service_hints(config));
    findings.extend(check_custom_services(config).await);
    findings.extend(check_worktree(config, project_root).await);
    findings.extend(check_devcontainer(config, project_root).await);

    let mut had_fail = false;
    for f in &findings {
        if f.severity == Severity::Fail {
            had_fail = true;
        }
        println!("{} {}", f.severity.marker(), f.message);
    }

    let summary = format!(
        "{} checks: {} ok, {} warn, {} fail",
        findings.len(),
        findings
            .iter()
            .filter(|f| f.severity == Severity::Ok)
            .count(),
        findings
            .iter()
            .filter(|f| f.severity == Severity::Warn)
            .count(),
        findings
            .iter()
            .filter(|f| f.severity == Severity::Fail)
            .count(),
    );
    println!("\n{summary}");

    Ok(if had_fail { 1 } else { 0 })
}

async fn check_backend(config: &Config) -> Vec<Finding> {
    use crate::config::model::Backend as B;
    match config.runtime.backend {
        B::None => vec![Finding::ok(
            "backend: none (no container backend configured)",
        )],
        B::Compose => match ComposeBackend::detect().await {
            Ok(b) => vec![Finding::ok(format!("backend: {} detected", b.name()))],
            Err(_) => vec![Finding::fail(
                "backend: docker compose / docker-compose not found on PATH",
            )],
        },
        B::Docker | B::Podman => vec![Finding::warn(format!(
            "backend: `{:?}` is configured but only `compose` is implemented in this version",
            config.runtime.backend
        ))],
    }
}

fn check_env_files(config: &Config, project_root: &Path) -> Vec<Finding> {
    if config.env_files.files.is_empty() {
        return vec![Finding::ok("env_files: none declared")];
    }
    let mut out = Vec::new();
    for raw in &config.env_files.files {
        // We don't expand ${VAR} here — doctor is best-effort and a literal
        // path mismatch is still useful signal.
        let path = project_root.join(raw);
        if path.exists() {
            out.push(Finding::ok(format!("env file `{raw}` present")));
        } else {
            out.push(Finding::warn(format!(
                "env file `{raw}` not found (will be skipped at runtime)"
            )));
        }
    }
    out
}

fn check_dependency_graph(config: &Config) -> Vec<Finding> {
    let mut out = Vec::new();
    let known: Vec<&String> = config
        .commands
        .keys()
        .chain(config.scripts.keys())
        .collect();

    let check_deps = |name: &str, deps: &[String], kind: &str| -> Vec<Finding> {
        let mut findings = Vec::new();
        for dep in deps {
            if !known.iter().any(|k| k.as_str() == dep) {
                findings.push(Finding::fail(format!(
                    "{kind} `{name}` depends on unknown `{dep}`"
                )));
            }
        }
        findings
    };

    for (name, recipe) in &config.commands {
        out.extend(check_deps(name, &recipe.needs, "recipe"));
    }
    for (name, script) in &config.scripts {
        out.extend(check_deps(name, &script.needs, "script"));
    }

    if out.is_empty() {
        out.push(Finding::ok(format!(
            "dependency graph: {} command(s), all `needs` resolve",
            known.len()
        )));
    }
    out
}

fn check_service_hints(config: &Config) -> Vec<Finding> {
    use crate::config::model::Backend as B;
    if !matches!(config.runtime.backend, B::Compose | B::Docker | B::Podman) {
        return Vec::new();
    }
    // We can't read docker-compose.yaml here without a YAML parser. So we
    // just count how many recipes declare `in =` and report it as info.
    let with_service = config
        .commands
        .values()
        .filter(|r| r.service.is_some())
        .count()
        + config
            .scripts
            .values()
            .filter(|s| s.service.is_some())
            .count();
    if with_service == 0 {
        Vec::new()
    } else {
        vec![Finding::ok(format!(
            "{with_service} command(s) target a container service (verify with `compose ps`)"
        ))]
    }
}

/// Probe each declared custom / systemd service: run its status
/// command (or the systemd `is-active` equivalent) and report the
/// outcome. We don't fail the project on a stopped service — the
/// whole point of a status check is "tell me whether it's running"
/// — but a missing systemctl binary or unknown unit gets surfaced
/// as a warning so the user has a chance to fix the config.
async fn check_custom_services(config: &Config) -> Vec<Finding> {
    use crate::runtime::services::{from_custom, from_systemd};

    let mut findings = Vec::new();
    let mut entries = Vec::new();
    for svc in &config.services.custom {
        entries.push(("custom", from_custom(svc)));
    }
    for svc in &config.services.systemd {
        entries.push(("systemd", from_systemd(svc)));
    }
    if entries.is_empty() {
        return findings;
    }

    for (kind, entry) in &entries {
        match probe_status(&entry.status_cmd).await {
            ProbeResult::Running => {
                findings.push(Finding::ok(format!(
                    "{kind} service `{}`: running",
                    entry.name
                )));
            }
            ProbeResult::Stopped(code) => {
                findings.push(Finding::ok(format!(
                    "{kind} service `{}`: stopped (status exit {code})",
                    entry.name
                )));
            }
            ProbeResult::SpawnFailed(msg) => {
                findings.push(Finding::warn(format!(
                    "{kind} service `{}`: status command failed to spawn ({msg})",
                    entry.name
                )));
            }
        }
    }
    findings
}

enum ProbeResult {
    Running,
    Stopped(i32),
    SpawnFailed(String),
}

async fn probe_status(cmd: &str) -> ProbeResult {
    use std::process::Stdio;
    use tokio::process::Command;
    match Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .stdin(Stdio::null())
        .status()
        .await
    {
        Ok(s) if s.success() => ProbeResult::Running,
        Ok(s) => ProbeResult::Stopped(s.code().unwrap_or(-1)),
        Err(e) => ProbeResult::SpawnFailed(e.to_string()),
    }
}

async fn check_worktree(config: &Config, project_root: &Path) -> Vec<Finding> {
    let identity = crate::runtime::Identity::detect(project_root, config).await;
    if !identity.is_isolated() {
        return vec![Finding::ok(
            "worktree: no git branch detected (offset = 0, no compose isolation)",
        )];
    }
    let pinned = config.worktrees.assign.contains_key(&identity.slug);
    let source = if pinned { "pinned" } else { "hashed" };
    vec![Finding::ok(format!(
        "worktree: slug `{}` → offset {} ({source}); compose isolation {}",
        identity.slug,
        identity.offset,
        if config.worktrees.isolate_compose {
            "on"
        } else {
            "off"
        },
    ))]
}

/// Devcontainer health check. Only runs when the user has opted in;
/// silent (skipped from output entirely) when disabled, so a normal
/// project's doctor output isn't padded with "[OK] devcontainer:
/// disabled" noise.
async fn check_devcontainer(config: &Config, project_root: &Path) -> Vec<Finding> {
    use crate::container::devcontainer::{ContainerSource, DevcontainerSpec};

    if !config.devcontainer.enabled {
        return Vec::new();
    }

    let mut out = Vec::new();
    let path = match DevcontainerSpec::discover(project_root, config.devcontainer.path.as_deref()) {
        Ok(p) => p,
        Err(e) => {
            out.push(Finding::fail(format!("devcontainer: {e}")));
            return out;
        }
    };
    out.push(Finding::ok(format!(
        "devcontainer: config at `{}`",
        path.display()
    )));

    let spec = match DevcontainerSpec::load(&path) {
        Ok(s) => s,
        Err(e) => {
            out.push(Finding::fail(format!("devcontainer: {e}")));
            return out;
        }
    };

    // Build source: validate dockerfile reachability so users don't
    // discover a typo on `ensure_up` (which would fail with a much
    // less specific error).
    if let ContainerSource::Build { dockerfile, .. } = &spec.source {
        if !dockerfile.is_file() {
            out.push(Finding::fail(format!(
                "devcontainer: `build.dockerfile` not found at `{}`",
                dockerfile.display()
            )));
        } else {
            out.push(Finding::ok(format!(
                "devcontainer: dockerfile `{}` exists",
                dockerfile.display()
            )));
        }
    }

    // Docker availability — without it the devcontainer is dead in
    // the water regardless of how good the spec is.
    if which::which("docker").is_err() {
        out.push(Finding::fail(
            "devcontainer: docker not found on PATH (install docker or set `[devcontainer] enabled = false`)",
        ));
        return out;
    }

    // Warn on privilege-escalating runArgs, mirroring the runtime
    // warning. Users may have opted in deliberately, but a quiet log
    // line at run time is easy to miss; doctor's job is to surface
    // the surprising.
    for arg in &spec.run_args {
        if arg == "--privileged"
            || arg == "--cap-add"
            || arg.starts_with("--cap-add=")
            || arg == "--network=host"
            || arg == "--net=host"
            || arg.starts_with("--network=host")
            || arg.starts_with("--net=host")
        {
            out.push(Finding::warn(format!(
                "devcontainer: runArgs includes `{arg}` (elevates privileges)"
            )));
        }
    }

    // Container state — informational, not pass/fail. `ensure_up`
    // will start a stopped one and create a missing one.
    let identity = crate::runtime::Identity::detect(project_root, config).await;
    let dc = match crate::cli::app::build_devcontainer(config, project_root, &identity) {
        Ok(Some(b)) => b,
        // Build returned None only when `enabled` is false, already
        // handled above; Err is caught by the earlier discover/load
        // arms. Defensive return.
        Ok(None) | Err(_) => return out,
    };
    let plan = dc.plan();
    out.push(Finding::ok(format!(
        "devcontainer: container `{}` → image `{}` → workspace `{}`",
        plan.container_name, plan.image_ref, plan.workspace_folder
    )));

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::parse_str;

    #[test]
    fn unknown_dep_is_fail() {
        let cfg = parse_str(
            r#"
            [command.test]
            run = "true"
            needs = ["build"]
        "#,
        )
        .unwrap();
        let findings = check_dependency_graph(&cfg);
        assert!(
            findings
                .iter()
                .any(|f| f.severity == Severity::Fail && f.message.contains("build"))
        );
    }

    #[test]
    fn known_dep_passes() {
        let cfg = parse_str(
            r#"
            [command.build]
            run = "true"
            [command.test]
            run = "true"
            needs = ["build"]
        "#,
        )
        .unwrap();
        let findings = check_dependency_graph(&cfg);
        assert!(findings.iter().all(|f| f.severity != Severity::Fail));
    }

    #[test]
    fn missing_env_file_is_warn() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg = parse_str(
            r#"
            [env_files]
            files = [".env.does-not-exist"]
        "#,
        )
        .unwrap();
        let findings = check_env_files(&cfg, dir.path());
        assert!(findings.iter().any(|f| f.severity == Severity::Warn));
    }
}