use std::{fmt::Debug, path::Path, process::Command};
use crate::logic::Backend;
pub enum ChecksError {
BothInstalled,
NotInstalled(Backend),
UnknownDependency,
NotInContainer,
}
impl Debug for ChecksError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChecksError::BothInstalled => {
write!(f, "Both backends are installed, uninstall on of them")
}
ChecksError::NotInstalled(backend) => write!(f, "Backend not installed: {backend:#?}"),
ChecksError::UnknownDependency => write!(f, "Unknown dependency"),
ChecksError::NotInContainer => write!(f, "Not in container: /app directory not found"),
}
}
}
#[derive(Default)]
pub struct Dependencies;
impl Dependencies {
pub fn new() -> Self {
Self::default()
}
fn whicher(bin: &str) -> Result<bool, ChecksError> {
match Command::new("which").arg(bin).output() {
Ok(_) => Ok(true),
Err(_) => Err(ChecksError::UnknownDependency),
}
}
pub fn installed(&self) -> Result<bool, ChecksError> {
if Self::whicher("docker")? && Self::whicher("podman")? {
return Err(ChecksError::BothInstalled);
} else if !Self::whicher("docker")? {
return Err(ChecksError::NotInstalled(Backend::Docker));
} else if !Self::whicher("podman")? {
return Err(ChecksError::NotInstalled(Backend::Podman));
} else {
Ok(true)
}
}
pub fn container(&self) -> Result<bool, ChecksError> {
if !Path::new("/app").exists() {
return Err(ChecksError::NotInContainer);
} else {
Ok(true)
}
}
}