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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Composite [`Backend`] that fans out across multiple owners.
//!
//! The TUI / runtime each hold a single `Arc<dyn Backend>`. The
//! registry preserves that uniformity while letting projects mix
//! container services (compose) with non-container services (custom
//! shell-driven, systemd, …) in a single dashboard. Per-method
//! routing rules:
//!
//! - `status`, `tail_logs`, `service_action(verb, [name])`: route to
//!   the owning backend (custom backend wins for its declared names;
//!   anything else falls through to the container backend).
//! - `service_action(verb, [])`: project-wide. Runs every owner, with
//!   the container backend's `Child` returned to the caller as the
//!   primary stream and any custom backend running in a detached
//!   task whose outcome surfaces via `tracing`. Phase D simplification
//!   — adequate for the common case (one owner has services) and
//!   honest about the edge (logged not lost).
//! - `list_services`: union of all owners.
//! - `exec`, `passthrough`, `exec_with_stdin`: container backend
//!   only; the registry forwards.

use crate::container::custom::CustomBackend;
use crate::container::{Backend, BackendError, ExecOptions, ServiceStatus};
use async_trait::async_trait;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::process::Child;
use tracing::{debug, warn};

/// Composite backend. `container` is optional so projects with only
/// custom services (`[runtime] backend = "none"` plus `services.custom`
/// or `services.systemd`) still work.
pub struct ServiceRegistry {
    container: Option<Arc<dyn Backend>>,
    custom: Option<Arc<CustomBackend>>,
    /// Cached set of names owned by `custom` for O(1) routing.
    custom_names: HashSet<String>,
}

impl ServiceRegistry {
    pub fn new(container: Option<Arc<dyn Backend>>, custom: Option<CustomBackend>) -> Self {
        let (custom, custom_names) = match custom {
            Some(c) if !c.is_empty() => {
                let names: HashSet<String> = c.names().map(String::from).collect();
                (Some(Arc::new(c)), names)
            }
            _ => (None, HashSet::new()),
        };
        Self {
            container,
            custom,
            custom_names,
        }
    }

    fn route(&self, service: &str) -> Option<Arc<dyn Backend>> {
        if self.custom_names.contains(service)
            && let Some(c) = &self.custom
        {
            return Some(Arc::clone(c) as Arc<dyn Backend>);
        }
        self.container.as_ref().map(Arc::clone)
    }

    fn require_container(&self) -> Result<Arc<dyn Backend>, BackendError> {
        self.container.as_ref().map(Arc::clone).ok_or_else(|| {
            BackendError::Reported(
                "no container backend configured (set runtime.backend = \"compose\")".into(),
            )
        })
    }
}

#[async_trait]
impl Backend for ServiceRegistry {
    fn name(&self) -> &'static str {
        "registry"
    }

    async fn status(&self, service: &str) -> Result<ServiceStatus, BackendError> {
        match self.route(service) {
            Some(b) => b.status(service).await,
            None => Ok(ServiceStatus::Missing),
        }
    }

    async fn exec(
        &self,
        service: &str,
        argv: &[&str],
        opts: &ExecOptions,
    ) -> Result<i32, BackendError> {
        // Custom services don't support exec; route to the named
        // owner so the user gets the helpful "<name> is a custom
        // service" message rather than a generic compose error.
        match self.route(service) {
            Some(b) => b.exec(service, argv, opts).await,
            None => self.require_container()?.exec(service, argv, opts).await,
        }
    }

    async fn exec_with_stdin(
        &self,
        service: &str,
        argv: &[&str],
        opts: &ExecOptions,
        stdin: &str,
    ) -> Result<i32, BackendError> {
        match self.route(service) {
            Some(b) => b.exec_with_stdin(service, argv, opts, stdin).await,
            None => {
                self.require_container()?
                    .exec_with_stdin(service, argv, opts, stdin)
                    .await
            }
        }
    }

    async fn spawn_exec(
        &self,
        service: &str,
        argv: &[&str],
        opts: &ExecOptions,
    ) -> Result<Child, BackendError> {
        match self.route(service) {
            Some(b) => b.spawn_exec(service, argv, opts).await,
            None => {
                self.require_container()?
                    .spawn_exec(service, argv, opts)
                    .await
            }
        }
    }

    async fn spawn_exec_with_stdin(
        &self,
        service: &str,
        argv: &[&str],
        opts: &ExecOptions,
    ) -> Result<Child, BackendError> {
        match self.route(service) {
            Some(b) => b.spawn_exec_with_stdin(service, argv, opts).await,
            None => {
                self.require_container()?
                    .spawn_exec_with_stdin(service, argv, opts)
                    .await
            }
        }
    }

    async fn passthrough(&self, args: &[&str]) -> Result<i32, BackendError> {
        self.require_container()?.passthrough(args).await
    }

    async fn list_services(&self) -> Result<Vec<String>, BackendError> {
        let mut out = Vec::new();
        if let Some(b) = &self.container {
            out.extend(b.list_services().await?);
        }
        if let Some(c) = &self.custom {
            out.extend(c.list_services().await?);
        }
        // Stable ordering: container services first (by their natural
        // order from compose), then custom (also natural order). The
        // TUI sorts on insertion anyway; this is for callers that
        // care.
        Ok(out)
    }

    async fn service_action(&self, action: &str, services: &[&str]) -> Result<Child, BackendError> {
        // Targeted action — partition by owner and dispatch.
        if !services.is_empty() {
            return self.dispatch_targeted(action, services).await;
        }

        // Project-wide. Common cases:
        //   only container has services → forward
        //   only custom has services → forward
        //   both have services → primary = container, custom runs
        //                        detached (logged via tracing)
        let has_container = self.container.is_some();
        let has_custom = self.custom.is_some();
        match (has_container, has_custom) {
            (true, false) => {
                self.container
                    .as_ref()
                    .unwrap()
                    .service_action(action, &[])
                    .await
            }
            (false, true) => {
                self.custom
                    .as_ref()
                    .unwrap()
                    .service_action(action, &[])
                    .await
            }
            (true, true) => self.dispatch_all_combined(action).await,
            (false, false) => Err(BackendError::Reported(
                "no backends configured for service action".into(),
            )),
        }
    }

    async fn tail_logs(&self, service: &str) -> Result<Child, BackendError> {
        match self.route(service) {
            Some(b) => b.tail_logs(service).await,
            None => self.require_container()?.tail_logs(service).await,
        }
    }
}

impl ServiceRegistry {
    /// Targeted fan-out: split `services` into the subset owned by
    /// each backend, dispatch to whichever backend has work. Errors
    /// when both backends would need to act on the same call (we
    /// can't return two `Child`s from one trait call); the user
    /// hits up/restart per-service in that rare case.
    async fn dispatch_targeted(
        &self,
        action: &str,
        services: &[&str],
    ) -> Result<Child, BackendError> {
        let (custom_names, container_names): (Vec<&str>, Vec<&str>) = services
            .iter()
            .copied()
            .partition(|n| self.custom_names.contains(*n));

        if !custom_names.is_empty() && !container_names.is_empty() {
            return Err(BackendError::Reported(format!(
                "mixed-backend action `{action}` ({} custom + {} container services); \
                 invoke per-service or use the project-wide action",
                custom_names.len(),
                container_names.len(),
            )));
        }

        if !custom_names.is_empty() {
            return self
                .custom
                .as_ref()
                .expect("custom_names nonempty implies custom backend present")
                .service_action(action, &custom_names)
                .await;
        }
        // All targets belong to the container backend.
        self.require_container()?
            .service_action(action, &container_names)
            .await
    }

    /// Project-wide action across both owners. Returns the container
    /// backend's `Child` as the primary stream the TUI tails; the
    /// custom backend's action is detached and its outcome is logged.
    async fn dispatch_all_combined(&self, action: &str) -> Result<Child, BackendError> {
        let container = self.container.as_ref().expect("checked by caller");
        let custom = Arc::clone(self.custom.as_ref().expect("checked by caller"));
        let action_owned = action.to_string();

        // Spawn the container action first so its Child is what we
        // return to the caller. The custom action runs in parallel —
        // logged but not surfaced to the TUI run pane in phase D.
        let primary = container.service_action(action, &[]).await?;

        tokio::spawn(async move {
            match custom.service_action(&action_owned, &[]).await {
                Ok(mut child) => match child.wait().await {
                    Ok(status) => {
                        debug!(
                            action = %action_owned,
                            exit = ?status.code(),
                            "custom backend action completed"
                        );
                    }
                    Err(e) => {
                        warn!(action = %action_owned, error = %e, "custom backend action wait failed")
                    }
                },
                Err(e) => {
                    warn!(action = %action_owned, error = %e, "custom backend action spawn failed")
                }
            }
        });

        Ok(primary)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::container::custom::CustomEntry;
    use crate::container::null::NullBackend;
    use std::sync::Mutex;

    fn entry(name: &str, status: &str, start: &str, stop: &str) -> CustomEntry {
        CustomEntry {
            name: name.into(),
            status_cmd: status.into(),
            start_cmd: start.into(),
            stop_cmd: stop.into(),
            restart_cmd: format!("{stop} && {start}"),
            logs_cmd: None,
        }
    }

    /// Backend that records every call so tests can assert routing.
    struct RecordingBackend {
        name_str: &'static str,
        services: Vec<String>,
        log: Mutex<Vec<String>>,
    }

    impl RecordingBackend {
        fn new(name: &'static str, services: Vec<&str>) -> Self {
            Self {
                name_str: name,
                services: services.into_iter().map(String::from).collect(),
                log: Mutex::new(Vec::new()),
            }
        }

        fn calls(&self) -> Vec<String> {
            self.log.lock().unwrap().clone()
        }
    }

    #[async_trait]
    impl Backend for RecordingBackend {
        fn name(&self) -> &'static str {
            self.name_str
        }
        async fn status(&self, service: &str) -> Result<ServiceStatus, BackendError> {
            self.log.lock().unwrap().push(format!("status:{service}"));
            Ok(ServiceStatus::Running)
        }
        async fn exec(
            &self,
            service: &str,
            _argv: &[&str],
            _opts: &ExecOptions,
        ) -> Result<i32, BackendError> {
            self.log.lock().unwrap().push(format!("exec:{service}"));
            Ok(0)
        }
        async fn passthrough(&self, args: &[&str]) -> Result<i32, BackendError> {
            self.log
                .lock()
                .unwrap()
                .push(format!("passthrough:{}", args.join(" ")));
            Ok(0)
        }
        async fn list_services(&self) -> Result<Vec<String>, BackendError> {
            Ok(self.services.clone())
        }
        async fn tail_logs(&self, service: &str) -> Result<Child, BackendError> {
            self.log
                .lock()
                .unwrap()
                .push(format!("tail_logs:{service}"));
            // Spawn `true` to satisfy the Child return shape.
            tokio::process::Command::new("true")
                .stdout(std::process::Stdio::piped())
                .stderr(std::process::Stdio::piped())
                .spawn()
                .map_err(BackendError::Spawn)
        }
        async fn service_action(
            &self,
            action: &str,
            services: &[&str],
        ) -> Result<Child, BackendError> {
            self.log
                .lock()
                .unwrap()
                .push(format!("action:{action}:{}", services.join(",")));
            tokio::process::Command::new("true")
                .stdout(std::process::Stdio::piped())
                .stderr(std::process::Stdio::piped())
                .spawn()
                .map_err(BackendError::Spawn)
        }
    }

    #[tokio::test]
    async fn list_services_unions_both_owners() {
        let container = Arc::new(RecordingBackend::new("rec", vec!["app", "worker"]));
        let custom = CustomBackend::new(vec![
            entry("ngrok", "true", ":", ":"),
            entry("postgres", "true", ":", ":"),
        ]);
        let registry = ServiceRegistry::new(Some(container), Some(custom));
        let mut listed = registry.list_services().await.unwrap();
        listed.sort();
        assert_eq!(listed, vec!["app", "ngrok", "postgres", "worker"]);
    }

    #[tokio::test]
    async fn status_routes_custom_to_custom_backend() {
        let container = Arc::new(RecordingBackend::new("rec", vec!["app"]));
        let custom = CustomBackend::new(vec![entry("ngrok", "true", ":", ":")]);
        let registry = ServiceRegistry::new(
            Some(Arc::clone(&container) as Arc<dyn Backend>),
            Some(custom),
        );
        registry.status("ngrok").await.unwrap();
        // Container backend never saw a status call for ngrok.
        assert!(container.calls().iter().all(|c| !c.contains("ngrok")));
    }

    #[tokio::test]
    async fn status_routes_compose_to_container_backend() {
        let container = Arc::new(RecordingBackend::new("rec", vec!["app"]));
        let custom = CustomBackend::new(vec![entry("ngrok", "true", ":", ":")]);
        let registry = ServiceRegistry::new(
            Some(Arc::clone(&container) as Arc<dyn Backend>),
            Some(custom),
        );
        registry.status("app").await.unwrap();
        assert!(container.calls().contains(&"status:app".to_string()));
    }

    #[tokio::test]
    async fn targeted_action_for_compose_only_routes_to_container() {
        let container = Arc::new(RecordingBackend::new("rec", vec!["app"]));
        let custom = CustomBackend::new(vec![entry("ngrok", "true", ":", ":")]);
        let registry = ServiceRegistry::new(
            Some(Arc::clone(&container) as Arc<dyn Backend>),
            Some(custom),
        );
        let _ = registry.service_action("up", &["app"]).await.unwrap();
        assert!(
            container
                .calls()
                .iter()
                .any(|c| c.starts_with("action:up:app"))
        );
    }

    #[tokio::test]
    async fn targeted_action_mixed_owners_errors_with_hint() {
        let container = Arc::new(RecordingBackend::new("rec", vec!["app"]));
        let custom = CustomBackend::new(vec![entry("ngrok", "true", ":", ":")]);
        let registry = ServiceRegistry::new(Some(container as Arc<dyn Backend>), Some(custom));
        let err = registry
            .service_action("up", &["app", "ngrok"])
            .await
            .unwrap_err();
        match err {
            BackendError::Reported(msg) => {
                assert!(msg.contains("mixed-backend"), "msg: {msg}");
            }
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[tokio::test]
    async fn project_wide_action_with_only_custom_uses_custom() {
        // No container backend at all — pure custom.
        let custom = CustomBackend::new(vec![entry("ngrok", "true", "true", "true")]);
        let registry = ServiceRegistry::new(None, Some(custom));
        let mut child = registry.service_action("up", &[]).await.unwrap();
        let status = child.wait().await.unwrap();
        assert!(status.success());
    }

    #[tokio::test]
    async fn passthrough_requires_container_backend() {
        let custom = CustomBackend::new(vec![entry("ngrok", "true", ":", ":")]);
        let registry = ServiceRegistry::new(None, Some(custom));
        let err = registry.passthrough(&["ps"]).await.unwrap_err();
        match err {
            BackendError::Reported(msg) => {
                assert!(msg.contains("no container backend"), "msg: {msg}");
            }
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[tokio::test]
    async fn empty_registry_returns_missing_for_unknown_service() {
        let registry = ServiceRegistry::new(Some(Arc::new(NullBackend) as Arc<dyn Backend>), None);
        // Null backend's status returns Missing for any name, since it
        // forwards to that backend via routing.
        let s = registry.status("anything").await.unwrap();
        assert_eq!(s, ServiceStatus::Missing);
    }
}