use microsandbox::sandbox::{RootfsSource, Sandbox, SandboxStatus};
use crate::ui;
pub mod common;
pub mod create;
pub mod exec;
pub mod image;
pub mod inspect;
pub mod install;
pub mod list;
pub mod metrics;
pub mod ps;
pub mod pull;
pub mod registry;
pub mod remove;
pub mod run;
pub mod self_cmd;
pub mod start;
pub mod stop;
pub mod uninstall;
pub mod volume;
pub async fn maybe_stop(sandbox: &Sandbox) {
if sandbox.owns_lifecycle()
&& let Err(e) = sandbox.stop_and_wait().await
{
ui::warn(&format!("failed to stop sandbox: {e}"));
}
}
pub async fn resolve_and_start(name: &str, quiet: bool) -> anyhow::Result<Sandbox> {
let handle = Sandbox::get(name).await?;
match handle.status() {
SandboxStatus::Running | SandboxStatus::Draining => {
Ok(handle.connect().await?)
}
SandboxStatus::Stopped | SandboxStatus::Crashed => {
if let Ok(config) = handle.config()
&& let RootfsSource::Oci(ref reference) = config.image
{
image::pull_if_missing(reference, quiet).await?;
}
let spinner = if quiet {
ui::Spinner::quiet()
} else {
ui::Spinner::start("Starting", name)
};
match handle.start().await {
Ok(s) => {
spinner.finish_clear();
Ok(s)
}
Err(e) => {
spinner.finish_error();
Err(e.into())
}
}
}
SandboxStatus::Paused => {
anyhow::bail!(
"sandbox '{}' is in state {:?} and cannot be started",
name,
handle.status()
);
}
}
}