Skip to main content

mk_lib/schema/command/
container_runtime.rs

1use std::path::PathBuf;
2
3use schemars::JsonSchema;
4use serde::{
5  Deserialize,
6  Serialize,
7};
8use which::which;
9
10#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
11#[serde(rename_all = "snake_case")]
12/// The container runtime to use.
13pub enum ContainerRuntime {
14  Auto,
15  Docker,
16  Podman,
17}
18
19impl ContainerRuntime {
20  pub fn resolve(runtime: Option<&ContainerRuntime>) -> anyhow::Result<PathBuf> {
21    match runtime.unwrap_or(&ContainerRuntime::Auto) {
22      ContainerRuntime::Auto => which("docker").or_else(|_| which("podman")).map_err(|_| {
23        anyhow::anyhow!(
24          "No container runtime found. Install Docker or Podman and ensure it is available in PATH."
25        )
26      }),
27      ContainerRuntime::Docker => which("docker")
28        .map_err(|_| anyhow::anyhow!("Docker not found. Install Docker and ensure it is available in PATH.")),
29      ContainerRuntime::Podman => which("podman")
30        .map_err(|_| anyhow::anyhow!("Podman not found. Install Podman and ensure it is available in PATH.")),
31    }
32  }
33
34  pub fn name(&self) -> &'static str {
35    match self {
36      ContainerRuntime::Auto => "auto",
37      ContainerRuntime::Docker => "docker",
38      ContainerRuntime::Podman => "podman",
39    }
40  }
41}