From 18f4a577bf732150692216420f5be436da54d7a6 Mon Sep 17 00:00:00 2001 From: Adam Perkowski Date: Mon, 25 Nov 2024 12:54:56 +0100 Subject: [PATCH] feat(take): `ALL` functionality Signed-off-by: Adam Perkowski --- man/nvrs.1 | 4 ++-- src/cli.rs | 4 ++-- src/main.rs | 28 +++++++++++++++++----------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/man/nvrs.1 b/man/nvrs.1 index ca90deb..48a9f83 100644 --- a/man/nvrs.1 +++ b/man/nvrs.1 @@ -19,11 +19,11 @@ Compare newver with oldver and display differences as updates .TP \fB\-t\fR, \fB\-\-take\fR \fI\fR -List of packages to update automatically, separated by a comma +Comma-separated list of packages to update automatically (use `ALL` for all) .TP \fB\-n\fR, \fB\-\-nuke\fR \fI\fR -List of packages to delete from the config, separated by a comma +Comma-separated list of packages to delete from the config .TP \fB\-\-config\fR \fI\fR diff --git a/src/cli.rs b/src/cli.rs index ed28248..5facb62 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -25,7 +25,7 @@ pub struct Cli { short = 't', long, value_name = "packages", - help = "List of packages to update automatically, separated by a comma", + help = "Comma-separated list of packages to update automatically (use `ALL` for all)", value_delimiter = ',' )] pub take: Option>, @@ -34,7 +34,7 @@ pub struct Cli { short = 'n', long, value_name = "packages", - help = "List of packages to delete from the config", + help = "Comma-separated list of packages to delete from the config", value_delimiter = ',' )] pub nuke: Option>, diff --git a/src/main.rs b/src/main.rs index d49f216..3ff9c0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,20 +79,26 @@ async fn take(core: Core, take_names: Option>) -> error::Result<()> let config = core.config; let (mut oldver, newver) = core.verfiles; - for package_name in names { - if let Some(new_pkg) = newver.data.data.iter().find(|p| p.0 == &package_name) { - if let Some(old_pkg) = oldver.data.data.iter_mut().find(|p| p.0 == &package_name) { - if old_pkg.1.version != new_pkg.1.version { + let packages_to_update = if names.contains(&"ALL".to_string()) { + newver.data.data.iter().map(|(name, _)| name.clone()).collect() + } else { + names + }; + + for package_name in packages_to_update { + if let Some(new_pkg) = newver.data.data.get(&package_name) { + if let Some(old_pkg) = oldver.data.data.get_mut(&package_name) { + if old_pkg.version != new_pkg.version { println!( "{} {} {} -> {}", "+".white().on_black(), package_name.blue(), - old_pkg.1.version.red(), - new_pkg.1.version.green() + old_pkg.version.red(), + new_pkg.version.green() ); - old_pkg.1.version = new_pkg.1.version.clone(); - old_pkg.1.gitref = new_pkg.1.gitref.clone(); - old_pkg.1.url = new_pkg.1.url.clone(); + old_pkg.version = new_pkg.version.clone(); + old_pkg.gitref = new_pkg.gitref.clone(); + old_pkg.url = new_pkg.url.clone(); } } else { println!( @@ -100,9 +106,9 @@ async fn take(core: Core, take_names: Option>) -> error::Result<()> "+".white().on_black(), package_name.blue(), "NONE".red(), - new_pkg.1.version.green() + new_pkg.version.green() ); - oldver.data.data.insert(package_name, new_pkg.1.clone()); + oldver.data.data.insert(package_name, new_pkg.clone()); } } else { return Err(error::Error::PkgNotInNewver(package_name));