From 1890d59428dfcca861ea1b7820411d80cc60d713 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 22 Jan 2023 22:34:17 +0100 Subject: Added list version cmd, fixed some todos --- src/commands/update.rs | 67 ++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index 068c3f3..f8bdb82 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,7 +1,6 @@ -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}, error::{MLE, MLError, ErrorType}}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; pub async fn update(config: Cfg, input: Input) -> MLE<()> { - let mut liststack: Vec = vec![]; if input.all_lists { let list_ids = lists_get_all_ids(config.clone())?; @@ -10,10 +9,9 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> { } } else { let current = get_current_list(config.clone())?; - println!("Checking for updates of mods in {}", current.id); + println!("Check for updates of mods in list {}", current.id); liststack.push(current) } - cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await } @@ -29,6 +27,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d let mut projects = projects(String::from(&config.apis.modrinth), mods).await; projects.sort_by_key(|pro| pro.id.clone()); + println!("Comparing mod versions:"); let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { //Get versions for project and check if they match up @@ -37,6 +36,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d let v_id = ¤t_version.mod_id; if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) }; + println!("\t({}) Check for update", project.title); + //Getting current installed version for disable or delete let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&project.id))?; @@ -49,40 +50,44 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d current_versions.push((disable_version, p_id)); ver }, - //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") - Err(..) => { - //Updating versions in modlist for no repeating version calls - mods_change_versions(config.clone(), version_db_string, project.id)?; - println!("({}) No new version found for the specified", project.title); + Err(e) => { + //Catch no update available + if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { + mods_change_versions(config.clone(), version_db_string, project.id)?; + println!("\t └No new version found for the specified minecraft version"); + } else { + return Err(e); + }; continue; }, }); } else { - println!("({}) No new version found", project.title); + println!("\t └No new version found"); }; }; + + //Linebreak readability + println!(""); - if clean { - let dl_path = ¤t_list.download_folder; - println!("Cleaning {}", dl_path); - for entry in std::fs::read_dir(dl_path)? { - let entry = entry?; - std::fs::remove_file(entry.path())?; - } - } + if clean { clean_list_dir(¤t_list)? }; + //Linebreak readability + println!(""); + if direct_download { - download_versions(current_list.clone(), updatestack).await?; + download_versions(current_list.clone(), config.clone(), updatestack).await?; //Disable old versions - for ver in current_versions { - if delete_old { - println!("Deleting version {} for mod {}", ver.0, ver.1); - delete_version(current_list.clone(), ver.0)?; - } else if ver.0 != "NONE" { - println!("Disabling version {} for mod {}", ver.0, ver.1); - disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; - }; + if !clean { + for ver in current_versions { + if delete_old { + println!("Deleting version {} for mod {}", ver.0, ver.1); + delete_version(current_list.clone(), ver.0)?; + } else if ver.0 != "NONE" { + println!("Disabling version {} for mod {}", ver.0, ver.1); + disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; + }; + } } }; @@ -92,8 +97,6 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d } async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE { - println!("Checking update for '{}' in {}", project.title, list.id); - let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; let mut versions: Vec = vec![]; @@ -110,7 +113,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) let mut current: Vec = vec![]; if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { //get new versions - print!(" | getting new version"); + println!("\t └Get versions for specified minecraft versions"); let current_str = extract_current_version(applicable_versions.clone())?; let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { Ok(v) => Ok(v), @@ -122,12 +125,12 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) Ok(p) => Ok(p), Err(e) => Err(MLError::new(ErrorType::Other, e)), }?.url; - userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id); + userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; } if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; - println!(" | ✔️"); + //println!(" └✔️"); Ok(current[0].clone()) } -- cgit v1.2.3