From 9063a041f6b2e72f6e4a861c77ac16065dd5378b Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 9 May 2023 20:34:09 +0200 Subject: add list toggles to update and download; add mod remove output and fix errors --- src/commands/download.rs | 18 ++++-------------- src/commands/modification.rs | 39 ++++++++++++++++++++++++++++++++++++--- src/commands/update.rs | 12 ++++++------ src/db.rs | 4 ++-- src/main.rs | 36 +++++++++++++++++++++++++++++------- 5 files changed, 77 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/commands/download.rs b/src/commands/download.rs index 1a8eb8f..ebfb4eb 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,6 +1,6 @@ -use crate::{config::Cfg, get_current_list, List}; +use crate::{config::Cfg, List}; use crate::{ - db::{lists_get, lists_get_all_ids, userlist_get_all_current_versions_with_mods}, + db::userlist_get_all_current_versions_with_mods, error::{ErrorType, MLError, MLE}, files::{ clean_list_dir, delete_version, disable_version, download_versions, get_downloaded_versions, @@ -8,20 +8,10 @@ use crate::{ modrinth::get_raw_versions, }; -pub async fn download(config: Cfg, all_lists: bool, clean: bool, delete_old: bool) -> MLE<()> { - let mut liststack: Vec = vec![]; - if all_lists { - let list_ids = lists_get_all_ids(config.clone())?; - for id in list_ids { - liststack.push(lists_get(config.clone(), id)?); - } - } else { - let current = get_current_list(config.clone())?; - println!("Downloading current versions of mods in {}", current.id); - liststack.push(current) - } +pub async fn download(config: Cfg, liststack: Vec, clean: bool, delete_old: bool) -> MLE<()> { for current_list in liststack { + println!("Downloading current versions of mods in {}", current_list.id); let downloaded_versions = get_downloaded_versions(current_list.clone())?; // println!("To download: {:#?}", downloaded_versions); let current_version_ids = match userlist_get_all_current_versions_with_mods( diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 67cde0b..216a06e 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,8 +1,10 @@ +use std::io::Write; + use crate::{ config::Cfg, db::{ lists_get_all_ids, mods_get_id, mods_insert, mods_remove, userlist_get_all_ids, - userlist_get_current_version, userlist_insert, userlist_remove, + userlist_get_current_version, userlist_insert, userlist_remove, mods_get_info, }, error::{ErrorType, MLError, MLE}, files::{delete_version, download_versions}, @@ -248,17 +250,47 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { let mod_id = mods_get_id(&config.data, id)?; + println!("Remove mod {} from {}", mods_get_info(config.clone(), &mod_id)?.title, list.id); let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; + print!(" └Remove from list"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; userlist_remove(config.clone(), &list.id, &mod_id)?; - delete_version(list, version)?; + println!(" ✓"); + + print!(" └Delete file"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; + match delete_version(list, version) { + Ok(_) => (), + Err(err) => { + if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { + return Err(err); + }; + () + }, + }; + println!(" ✓"); + print!(" └Clean main db table"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; let list_ids = lists_get_all_ids(config.clone())?; // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { - let mods = userlist_get_all_ids(config.clone(), id)?; + let mods = match userlist_get_all_ids(config.clone(), id) { + Ok(m) => m, + Err(err) => { + if err.to_string() == "Database: NO_MODS_USERLIST" { + println!(" ✓"); + return Ok(()); + }; + return Err(err) + } + }; if mods.contains(&mod_id) { mod_used = true; break; @@ -268,6 +300,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { if !mod_used { mods_remove(config, mod_id)?; }; + println!(" ✓"); Ok(()) } diff --git a/src/commands/update.rs b/src/commands/update.rs index 4bc3ac0..d76ba4b 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -18,19 +18,19 @@ pub async fn update( delete_old: bool, ) -> MLE<()> { for current_list in liststack { + println!("Update mods in {}", current_list.id); let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; let mut current_versions: Vec<(String, String)> = vec![]; - println!(" └Update mods:"); let mut updatestack: Vec = vec![]; for id in mods { let info = mods_get_info(config.clone(), &id)?; - println!("\t└{}", info.title); + println!(" └{}", info.title); if userlist_get_set_version(config.clone(), ¤t_list.id, &id)? { - println!("\t └Set version, skipping update"); + println!(" └Set version, skipping update"); continue; } @@ -54,7 +54,7 @@ pub async fn update( Err(e) => { if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { println!( - "\t └No new version found for the specified minecraft version" + " └No new version found for the specified minecraft version" ); } else { return Err(e); @@ -76,10 +76,10 @@ pub async fn update( if !clean { for ver in current_versions { if delete_old { - println!("\t └Delete version {}", ver.0); + println!(" └Delete version {}", ver.0); delete_version(current_list.clone(), ver.0)?; } else if ver.0 != "NONE" { - println!("\t └Disable version {}", ver.0); + println!(" └Disable version {}", ver.0); disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; }; } diff --git a/src/db.rs b/src/db.rs index 6c7e4f8..9ffbfe5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -35,7 +35,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result, Box Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS"))), + true => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))), false => Ok(mods), } } @@ -234,7 +234,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE> { } match mod_ids.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "NO_MODS")), + true => Err(MLError::new(ErrorType::DBError, "NO_MODS_USERLIST")), false => Ok(mod_ids), } } diff --git a/src/main.rs b/src/main.rs index 3bc2ba0..2db304b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,10 @@ enum Commands { /// remove disabled versions #[arg(short, long)] remove: bool, + + /// optional List selection, else default list will be used + #[arg(short, long)] + list: Option, }, Update { /// download all lists @@ -61,6 +65,10 @@ enum Commands { /// delete disabled versions #[arg(short, long)] remove: bool, + + /// optional List selection, else default list will be used + #[arg(short, long)] + list: Option, }, Import { #[arg(short, long)] @@ -180,8 +188,6 @@ async fn main() { mod_add(config, vec![marked_id], listf, download, lock).await } ModCommands::Remove { id, list } => { - //TODO add output - //TODO add success even if no file found let listf = match list { Some(list) => lists_get(config.clone(), list).unwrap(), None => lists_get( @@ -228,12 +234,12 @@ async fn main() { } => list_version(config, id, version, download, remove).await, } } - //TODO a add specific list Commands::Update { all, download, clean, remove, + list } => { let mut liststack: Vec = vec![]; if all { @@ -242,14 +248,30 @@ async fn main() { liststack.push(lists_get(config.clone(), id).unwrap()); } } else { - let current = get_current_list(config.clone()).unwrap(); - println!("Update list {}:", current.id); + let current = match list { + Some(l) => lists_get(config.clone(), l).unwrap(), + None => get_current_list(config.clone()).unwrap(), + }; liststack.push(current) } update(config, liststack, clean, download, remove).await } - //TODO add specific list - Commands::Download { all, clean, remove } => download(config, all, clean, remove).await, + Commands::Download { all, clean, remove, list } => { + let mut liststack: Vec = vec![]; + if all { + let list_ids = lists_get_all_ids(config.clone()).unwrap(); + for id in list_ids { + liststack.push(lists_get(config.clone(), id).unwrap()); + } + } else { + let current = match list { + Some(l) => lists_get(config.clone(), l).unwrap(), + None => get_current_list(config.clone()).unwrap(), + }; + liststack.push(current) + } + download(config, liststack, clean, remove).await + }, Commands::Import { file, download } => { let filestr: String = match file { Some(args) => args, -- cgit v1.2.3