use crate::Result;
use crate::cli::daemons::resolve_project_config_path;
use crate::daemon_id::DaemonId;
use crate::pitchfork_toml::{PitchforkToml, namespace_from_path};
use miette::IntoDiagnostic;
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "rm", verbatim_doc_comment)]
pub struct Remove {
id: String,
#[clap(long)]
local: bool,
#[clap(long)]
project: bool,
}
impl Remove {
pub async fn run(&self) -> Result<()> {
let path = resolve_project_config_path(self.local, self.project, true).await?;
if !tokio::fs::try_exists(&path).await.unwrap_or(false) {
if self.local {
warn!("No pitchfork.local.toml found");
} else {
warn!("No project pitchfork.toml files found");
}
return Ok(());
}
let mut pt = {
let path_clone = path.clone();
let result = tokio::task::spawn_blocking(move || PitchforkToml::read(&path_clone))
.await
.into_diagnostic()?;
result.map_err(|e| miette::miette!("{e}"))?
};
let canonical_path = tokio::fs::canonicalize(&path)
.await
.unwrap_or_else(|_| path.clone());
let daemon_id = if self.id.contains('/') {
DaemonId::parse(&self.id)?
} else {
let namespace = namespace_from_path(&canonical_path)?;
DaemonId::try_new(&namespace, &self.id)?
};
if pt.daemons.shift_remove(&daemon_id).is_some() {
tokio::task::spawn_blocking(move || pt.write())
.await
.into_diagnostic()?
.map_err(|e| miette::miette!("{e}"))?;
println!("removed {} from {}", daemon_id, path.display());
} else {
warn!("{} not found in {}", daemon_id, path.display());
}
Ok(())
}
}