Use --discard

This commit is contained in:
sxyazi 2026-03-20 15:19:22 +08:00
parent 82f4222385
commit 1dbf9f3f98
No known key found for this signature in database
9 changed files with 30 additions and 30 deletions

View file

@ -77,16 +77,16 @@ pub(super) enum CommandPkg {
Delete { Delete {
/// Packages to delete. /// Packages to delete.
#[arg(index = 1, num_args = 1..)] #[arg(index = 1, num_args = 1..)]
ids: Vec<String>, ids: Vec<String>,
/// Overwrite any local changes made to packages. /// Discard any local changes made to packages.
#[arg(long)] #[arg(long)]
overwrite: bool, discard: bool,
}, },
/// Install all packages. /// Install all packages.
Install { Install {
/// Overwrite any local changes made to packages. /// Discard any local changes made to packages.
#[arg(long)] #[arg(long)]
overwrite: bool, discard: bool,
}, },
/// List all packages. /// List all packages.
List, List,
@ -94,10 +94,10 @@ pub(super) enum CommandPkg {
Upgrade { Upgrade {
/// Packages to upgrade, upgrade all if unspecified. /// Packages to upgrade, upgrade all if unspecified.
#[arg(index = 1, num_args = 0..)] #[arg(index = 1, num_args = 0..)]
ids: Vec<String>, ids: Vec<String>,
/// Overwrite any local changes made to packages. /// Discard any local changes made to packages.
#[arg(short, long)] #[arg(short, long)]
overwrite: bool, discard: bool,
}, },
} }

View file

@ -78,10 +78,10 @@ async fn run() -> anyhow::Result<()> {
let mut pkg = package::Package::load().await?; let mut pkg = package::Package::load().await?;
match cmd { match cmd {
CommandPkg::Add { ids } => pkg.add_many(&ids).await?, CommandPkg::Add { ids } => pkg.add_many(&ids).await?,
CommandPkg::Delete { ids, overwrite } => pkg.delete_many(&ids, overwrite).await?, CommandPkg::Delete { ids, discard } => pkg.delete_many(&ids, discard).await?,
CommandPkg::Install { overwrite } => pkg.install(overwrite).await?, CommandPkg::Install { discard } => pkg.install(discard).await?,
CommandPkg::List => pkg.print()?, CommandPkg::List => pkg.print()?,
CommandPkg::Upgrade { ids, overwrite } => pkg.upgrade_many(&ids, overwrite).await?, CommandPkg::Upgrade { ids, discard } => pkg.upgrade_many(&ids, discard).await?,
} }
} }

View file

@ -4,7 +4,7 @@ use super::{Dependency, Git};
use crate::shared::must_exists; use crate::shared::must_exists;
impl Dependency { impl Dependency {
pub(super) async fn add(&mut self, overwrite: bool) -> Result<()> { pub(super) async fn add(&mut self, discard: bool) -> Result<()> {
self.header("Upgrading package `{name}`")?; self.header("Upgrading package `{name}`")?;
let path = self.local(); let path = self.local();
@ -14,7 +14,7 @@ impl Dependency {
Git::clone(&self.remote(), &path).await?; Git::clone(&self.remote(), &path).await?;
}; };
self.deploy(overwrite).await?; self.deploy(discard).await?;
self.rev = Git::revision(&path).await?; self.rev = Git::revision(&path).await?;
Ok(()) Ok(())
} }

View file

@ -6,13 +6,13 @@ use super::Dependency;
use crate::shared::{maybe_exists, remove_sealed}; use crate::shared::{maybe_exists, remove_sealed};
impl Dependency { impl Dependency {
pub(super) async fn delete(&self, overwrite: bool) -> Result<()> { pub(super) async fn delete(&self, discard: bool) -> Result<()> {
self.header("Deleting package `{name}`")?; self.header("Deleting package `{name}`")?;
let dir = self.target(); let dir = self.target();
if !maybe_exists(&dir).await { if !maybe_exists(&dir).await {
return Ok(outln!("Not found, skipping")?); return Ok(outln!("Not found, skipping")?);
} else if !overwrite { } else if !discard {
self.hash_check().await?; self.hash_check().await?;
} }

View file

@ -8,7 +8,7 @@ use super::Dependency;
use crate::shared::{copy_and_seal, maybe_exists}; use crate::shared::{copy_and_seal, maybe_exists};
impl Dependency { impl Dependency {
pub(super) async fn deploy(&mut self, overwrite: bool) -> Result<()> { pub(super) async fn deploy(&mut self, discard: bool) -> Result<()> {
let from = self.local().join(&self.child); let from = self.local().join(&self.child);
self.header("Deploying package `{name}`")?; self.header("Deploying package `{name}`")?;
@ -16,7 +16,7 @@ impl Dependency {
let to = self.target(); let to = self.target();
let exists = maybe_exists(&to).await; let exists = maybe_exists(&to).await;
if exists && !overwrite { if exists && !discard {
self.hash_check().await?; self.hash_check().await?;
} }

View file

@ -46,7 +46,7 @@ impl Dependency {
if self.hash != self.hash().await? { if self.hash != self.hash().await? {
bail!( bail!(
"You have modified the contents of the `{}` {}. For safety, the operation has been aborted. "You have modified the contents of the `{}` {}. For safety, the operation has been aborted.
Please manually delete it from `{}` and re-run the command, or add `--overwrite` to overwrite any local changes.", Please manually delete it from `{}` and re-run the command, or add `--discard` to discard any local changes.",
self.name, self.name,
if self.is_flavor { "flavor" } else { "plugin" }, if self.is_flavor { "flavor" } else { "plugin" },
self.target().display() self.target().display()

View file

@ -4,7 +4,7 @@ use super::{Dependency, Git};
use crate::shared::must_exists; use crate::shared::must_exists;
impl Dependency { impl Dependency {
pub(super) async fn install(&mut self, overwrite: bool) -> Result<()> { pub(super) async fn install(&mut self, discard: bool) -> Result<()> {
self.header("Fetching package `{name}`")?; self.header("Fetching package `{name}`")?;
let path = self.local(); let path = self.local();
@ -18,7 +18,7 @@ impl Dependency {
Git::checkout(&path, self.rev.trim_start_matches('=')).await?; Git::checkout(&path, self.rev.trim_start_matches('=')).await?;
} }
self.deploy(overwrite).await?; self.deploy(discard).await?;
if self.rev.is_empty() { if self.rev.is_empty() {
self.rev = Git::revision(&path).await?; self.rev = Git::revision(&path).await?;
} }

View file

@ -28,19 +28,19 @@ impl Package {
Ok(()) Ok(())
} }
pub(crate) async fn delete_many(&mut self, uses: &[String], overwrite: bool) -> Result<()> { pub(crate) async fn delete_many(&mut self, uses: &[String], discard: bool) -> Result<()> {
for u in uses { for u in uses {
let r = self.delete(u, overwrite).await; let r = self.delete(u, discard).await;
self.save().await?; self.save().await?;
r?; r?;
} }
Ok(()) Ok(())
} }
pub(crate) async fn install(&mut self, overwrite: bool) -> Result<()> { pub(crate) async fn install(&mut self, discard: bool) -> Result<()> {
macro_rules! go { macro_rules! go {
($dep:expr) => { ($dep:expr) => {
let r = $dep.install(overwrite).await; let r = $dep.install(discard).await;
self.save().await?; self.save().await?;
r?; r?;
}; };
@ -55,11 +55,11 @@ impl Package {
Ok(()) Ok(())
} }
pub(crate) async fn upgrade_many(&mut self, uses: &[String], overwrite: bool) -> Result<()> { pub(crate) async fn upgrade_many(&mut self, uses: &[String], discard: bool) -> Result<()> {
macro_rules! go { macro_rules! go {
($dep:expr) => { ($dep:expr) => {
if uses.is_empty() || uses.contains(&$dep.r#use) { if uses.is_empty() || uses.contains(&$dep.r#use) {
let r = $dep.upgrade(overwrite).await; let r = $dep.upgrade(discard).await;
self.save().await?; self.save().await?;
r?; r?;
} }
@ -116,12 +116,12 @@ impl Package {
Ok(()) Ok(())
} }
async fn delete(&mut self, r#use: &str, overwrite: bool) -> Result<()> { async fn delete(&mut self, r#use: &str, discard: bool) -> Result<()> {
let Some(dep) = self.identical(&Dependency::from_str(r#use)?).cloned() else { let Some(dep) = self.identical(&Dependency::from_str(r#use)?).cloned() else {
bail!("`{}` was not found in package.toml", r#use) bail!("`{}` was not found in package.toml", r#use)
}; };
dep.delete(overwrite).await?; dep.delete(discard).await?;
if dep.is_flavor { if dep.is_flavor {
self.flavors.retain(|d| !d.identical(&dep)); self.flavors.retain(|d| !d.identical(&dep));
} else { } else {

View file

@ -3,7 +3,7 @@ use anyhow::Result;
use super::Dependency; use super::Dependency;
impl Dependency { impl Dependency {
pub(super) async fn upgrade(&mut self, overwrite: bool) -> Result<()> { pub(super) async fn upgrade(&mut self, discard: bool) -> Result<()> {
if self.rev.starts_with('=') { Ok(()) } else { self.add(overwrite).await } if self.rev.starts_with('=') { Ok(()) } else { self.add(discard).await }
} }
} }