use std::path::PathBuf;
use podup::compose::types::ComposeFile;
use podup::ComposeError;
use crate::cli::{AutostartCommands, AutostartMode};
pub(crate) struct AutostartEnv<'a> {
pub profile: &'a [String],
pub env_files: &'a [String],
pub socket: Option<String>,
}
pub(crate) async fn dispatch(
env: &AutostartEnv<'_>,
compose_files: &[PathBuf],
project: String,
base_dir: PathBuf,
file: &ComposeFile,
kind: &AutostartCommands,
) -> podup::Result<()> {
match kind {
AutostartCommands::Install {
mode: AutostartMode::Quadlet,
no_start,
dry_run,
} => {
let base_dir = std::fs::canonicalize(&base_dir).unwrap_or(base_dir);
podup::autostart::install_quadlet(
&podup::autostart::RealSystemCtl,
file,
&project,
&base_dir,
*no_start,
*dry_run,
)
}
AutostartCommands::Install {
mode: AutostartMode::Service,
no_start,
dry_run,
} => {
let exe = std::env::current_exe().map_err(|e| {
ComposeError::Autostart(format!("cannot locate the podup executable: {e}"))
})?;
let exe = std::fs::canonicalize(&exe).unwrap_or(exe);
let mut abs_files = Vec::with_capacity(compose_files.len());
for f in compose_files {
abs_files.push(std::fs::canonicalize(f).map_err(|e| {
ComposeError::Autostart(format!(
"cannot resolve compose file {}: {e}",
f.display()
))
})?);
}
let working_dir = std::fs::canonicalize(&base_dir).unwrap_or(base_dir);
let opts = podup::autostart::InstallOptions {
unit: podup::autostart::ServiceUnitOpts {
exe,
compose_files: abs_files,
project,
working_dir,
profiles: env.profile.to_vec(),
env_files: env.env_files.to_vec(),
},
no_start: *no_start,
dry_run: *dry_run,
};
podup::autostart::install(&podup::autostart::RealSystemCtl, &opts)
}
AutostartCommands::Uninstall { purge } => {
let uninstalled = match podup::autostart::installed_mode(&project) {
podup::autostart::InstalledMode::Quadlet => {
podup::autostart::uninstall_quadlet(&podup::autostart::RealSystemCtl, &project)
}
_ => podup::autostart::uninstall(&podup::autostart::RealSystemCtl, &project),
};
if *purge {
let client = podup::podman::connect(env.socket.as_deref())?;
let engine = podup::Engine::with_base_dir(client, project, base_dir);
let _lock = engine.lock_project()?;
engine.down_with_options(file, true).await?;
}
uninstalled
}
AutostartCommands::Status => {
podup::autostart::status(&podup::autostart::RealSystemCtl, &project)
}
AutostartCommands::Rebuild { service } => podup::autostart::rebuild_quadlet(
&podup::autostart::RealSystemCtl,
&project,
service.as_deref(),
),
}
}