mirror of
https://github.com/pesde-pkg/pesde.git
synced 2025-04-19 03:13:51 +01:00
59 lines
1.2 KiB
Rust
59 lines
1.2 KiB
Rust
use std::str::FromStr;
|
|
|
|
use anyhow::Context;
|
|
use clap::Args;
|
|
|
|
use crate::cli::{
|
|
dep_type_to_key,
|
|
style::{INFO_STYLE, SUCCESS_STYLE},
|
|
};
|
|
use pesde::{
|
|
manifest::{Alias, DependencyType},
|
|
Project,
|
|
};
|
|
|
|
#[derive(Debug, Args)]
|
|
pub struct RemoveCommand {
|
|
/// The alias of the package to remove
|
|
#[arg(index = 1)]
|
|
alias: Alias,
|
|
}
|
|
|
|
impl RemoveCommand {
|
|
pub async fn run(self, project: Project) -> anyhow::Result<()> {
|
|
let mut manifest = toml_edit::DocumentMut::from_str(
|
|
&project
|
|
.read_manifest()
|
|
.await
|
|
.context("failed to read manifest")?,
|
|
)
|
|
.context("failed to parse manifest")?;
|
|
|
|
let Some(dep_key) = DependencyType::VARIANTS
|
|
.iter()
|
|
.copied()
|
|
.map(dep_type_to_key)
|
|
.find(|dependency_key| {
|
|
manifest[dependency_key]
|
|
.as_table_mut()
|
|
.is_some_and(|table| table.remove(self.alias.as_str()).is_some())
|
|
})
|
|
else {
|
|
anyhow::bail!("package under alias `{}` not found in manifest", self.alias)
|
|
};
|
|
|
|
project
|
|
.write_manifest(manifest.to_string())
|
|
.await
|
|
.context("failed to write manifest")?;
|
|
|
|
println!(
|
|
"{} removed {} from {}!",
|
|
SUCCESS_STYLE.apply_to("success!"),
|
|
INFO_STYLE.apply_to(self.alias),
|
|
INFO_STYLE.apply_to(dep_key)
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
}
|