Skip to main content

flodl_cli/util/
docker.rs

1//! Docker and Docker Compose interaction.
2
3use std::process::{Command, ExitStatus, Output, Stdio};
4
5/// Check whether Docker is available and the daemon is running.
6pub fn has_docker() -> bool {
7    Command::new("docker")
8        .arg("info")
9        .stdout(Stdio::null())
10        .stderr(Stdio::null())
11        .status()
12        .is_ok_and(|s| s.success())
13}
14
15/// Check whether a Docker image exists locally.
16#[allow(dead_code)]
17pub fn image_exists(name: &str) -> bool {
18    Command::new("docker")
19        .args(["image", "inspect", name])
20        .stdout(Stdio::null())
21        .stderr(Stdio::null())
22        .status()
23        .is_ok_and(|s| s.success())
24}
25
26/// Run `docker compose <args>` with stdout/stderr inherited (streaming).
27///
28/// Returns the exit status. The `compose_dir` is set as the working directory.
29#[allow(dead_code)]
30pub fn compose_run(compose_dir: &str, args: &[&str]) -> Result<ExitStatus, String> {
31    Command::new("docker")
32        .arg("compose")
33        .args(args)
34        .current_dir(compose_dir)
35        .stdout(Stdio::inherit())
36        .stderr(Stdio::inherit())
37        .status()
38        .map_err(|e| format!("failed to run docker compose: {}", e))
39}
40
41/// Run `docker compose <args>` and capture output.
42#[allow(dead_code)]
43pub fn compose_output(compose_dir: &str, args: &[&str]) -> Result<Output, String> {
44    Command::new("docker")
45        .arg("compose")
46        .args(args)
47        .current_dir(compose_dir)
48        .output()
49        .map_err(|e| format!("failed to run docker compose: {}", e))
50}
51
52/// Run `docker <args>` and capture output.
53pub fn docker_output(args: &[&str]) -> Result<Output, String> {
54    Command::new("docker")
55        .args(args)
56        .output()
57        .map_err(|e| format!("failed to run docker: {}", e))
58}
59
60/// Run `docker <args>` with streaming output.
61pub fn docker_run(args: &[&str]) -> Result<ExitStatus, String> {
62    Command::new("docker")
63        .args(args)
64        .stdout(Stdio::inherit())
65        .stderr(Stdio::inherit())
66        .status()
67        .map_err(|e| format!("failed to run docker: {}", e))
68}