use anyhow::Result;
use std::process::Stdio;
use tokio::process::Command;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContainerRuntime {
Docker,
Podman,
}
impl ContainerRuntime {
pub fn command(&self) -> &'static str {
match self {
ContainerRuntime::Docker => "docker",
ContainerRuntime::Podman => "podman",
}
}
}
pub async fn detect_runtime() -> Result<ContainerRuntime> {
if Command::new("docker")
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.await
.map(|s| s.success())
.unwrap_or(false)
{
return Ok(ContainerRuntime::Docker);
}
if Command::new("podman")
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.await
.map(|s| s.success())
.unwrap_or(false)
{
return Ok(ContainerRuntime::Podman);
}
Err(anyhow::anyhow!(
"No container runtime found. Please install Docker or Podman."
))
}
pub async fn get_runtime(preferred: Option<&str>) -> Result<ContainerRuntime> {
match preferred {
Some("docker") => Ok(ContainerRuntime::Docker),
Some("podman") => Ok(ContainerRuntime::Podman),
_ => detect_runtime().await,
}
}