Skip to main content

mk_lib/schema/command/
container_runtime.rs

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