use crate::config::schema::VmConfig;
use crate::config::Config;
use crate::error::{MinoError, MinoResult};
use crate::orchestration::native_podman::NativePodmanRuntime;
use crate::orchestration::orbstack_runtime::OrbStackRuntime;
use crate::orchestration::runtime::ContainerRuntime;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Platform {
MacOS,
Linux,
Unsupported,
}
impl Platform {
pub fn detect() -> Self {
match std::env::consts::OS {
"macos" => Platform::MacOS,
"linux" => Platform::Linux,
_ => Platform::Unsupported,
}
}
pub fn name(&self) -> &'static str {
match self {
Platform::MacOS => "macOS",
Platform::Linux => "Linux",
Platform::Unsupported => "Unsupported",
}
}
}
pub fn create_runtime(config: &Config) -> MinoResult<Box<dyn ContainerRuntime>> {
match Platform::detect() {
Platform::MacOS => Ok(Box::new(OrbStackRuntime::new(config.vm.clone()))),
Platform::Linux => Ok(Box::new(NativePodmanRuntime::new())),
Platform::Unsupported => Err(MinoError::UnsupportedPlatform(
std::env::consts::OS.to_string(),
)),
}
}
pub fn create_runtime_with_vm(vm_config: VmConfig) -> MinoResult<Box<dyn ContainerRuntime>> {
match Platform::detect() {
Platform::MacOS => Ok(Box::new(OrbStackRuntime::new(vm_config))),
Platform::Linux => Ok(Box::new(NativePodmanRuntime::new())),
Platform::Unsupported => Err(MinoError::UnsupportedPlatform(
std::env::consts::OS.to_string(),
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn platform_detect_returns_valid() {
let platform = Platform::detect();
assert!(matches!(
platform,
Platform::MacOS | Platform::Linux | Platform::Unsupported
));
}
#[test]
fn platform_name() {
assert_eq!(Platform::MacOS.name(), "macOS");
assert_eq!(Platform::Linux.name(), "Linux");
assert_eq!(Platform::Unsupported.name(), "Unsupported");
}
#[test]
fn create_runtime_succeeds_on_supported_platform() {
let config = Config::default();
let result = create_runtime(&config);
match Platform::detect() {
Platform::MacOS | Platform::Linux => {
assert!(result.is_ok());
}
Platform::Unsupported => {
assert!(result.is_err());
}
}
}
}