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/download.rs | 13 ++------- src/commands/list.rs | 30 +++++++++----------- src/commands/mod.rs | 4 +-- src/commands/modification.rs | 45 +++++++++++------------------ src/commands/setup.rs | 2 +- src/commands/update.rs | 67 +++++++++++++++++++++++--------------------- 6 files changed, 72 insertions(+), 89 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 0f63876..7748d15 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,4 +1,4 @@ -use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}}; +use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version, clean_list_dir}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}}; use crate::{List, get_current_list, config::Cfg, input::Input}; pub async fn download(config: Cfg, input: Input) -> MLE<()> { @@ -44,17 +44,10 @@ pub async fn download(config: Cfg, input: Input) -> MLE<()> { } } - if input.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 input.clean { clean_list_dir(¤t_list)? }; if !to_download.is_empty() { - download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; + download_versions(current_list.clone(), config.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; } else { println!("There are no new versions to download"); } diff --git a/src/commands/list.rs b/src/commands/list.rs index bc58787..eaf6fa1 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,4 +1,4 @@ -use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::MLE, /*modrinth::MCVersionType*/}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, ListOptions}, cmd_update, error::MLE}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -20,13 +20,9 @@ pub async fn list(config: Cfg, input: Input) -> MLE<()> { ListOptions::Remove => { remove(config, input) }, - /* - Subcmd::Version => { - match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { - Ok(..) => Ok(()), - Err(e) => Err(Box::new(e)) - } - }*/ + ListOptions::Version => { + version(config, input).await + } } } @@ -44,7 +40,7 @@ fn add(config: Cfg, input: Input) -> MLE<()> { } fn change(config: Cfg, input: Input) -> MLE<()> { - //TODO reimplement current list + println!("Change default list to: {}", input.clone().list.unwrap().id); config_change_current_list(config, input.list.unwrap().id) } @@ -52,17 +48,19 @@ fn remove(config: Cfg, input: Input) -> MLE<()> { lists_remove(config, input.list.unwrap().id) } -/* ///Changing the current lists version and updating it /// #Arguments /// /// * `config` - The current config /// * `args` - All args, to extract the new version -async fn version(config: Cfg, args: Option>, version_type: Option) -> MLE<()> { - let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?; +async fn version(config: Cfg, input: Input) -> MLE<()> { + println!("Change version for list {} to minecraft version: {}", input.clone().list.unwrap().id, input.clone().list_mcversion.unwrap()); - lists_version(config.clone(), String::from(¤t_list.id), String::from(&args.unwrap()[0]))?; - //update the list & with -- args - cmd_update(config, vec![current_list], true, true, false).await + lists_version(config.clone(), input.clone().list.ok_or("").unwrap().id, input.clone().list_mcversion.ok_or("").unwrap())?; + + //Linebreak readability + println!(""); + + println!("Check for updates for new minecraft version in list {}", input.clone().list.unwrap().id); + cmd_update(config, vec![input.list.ok_or("").unwrap()], true, input.direct_download, input.delete_old).await } -*/ diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 527afc7..38139f9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,11 +1,11 @@ -//pub mod modification; +pub mod modification; pub mod list; pub mod update; //pub mod setup; pub mod download; pub mod io; -//pub use modification::*; +pub use modification::*; pub use list::*; pub use update::*; //pub use setup::*; diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 7d4be8d..6a03b35 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,33 +1,24 @@ -use std::io::{Error, ErrorKind}; +use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; -use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; - -pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.as_ref().ok_or("")? { - Subcmd::Add => { +pub async fn modification(config: Cfg, input: Input) -> MLE<()> { + match input.clone().mod_options.ok_or("").unwrap() { + ModOptions::Add => { add(config, input).await }, - Subcmd::Remove => { - remove(config, input.args.ok_or("")?) + ModOptions::Remove => { + remove(config, input) }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE"))) } } -async fn add(config: Cfg, input: Input) -> Result<(), Box> { - - let args = input.args.ok_or("")?; +async fn add(config: Cfg, input: Input) -> MLE<()> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - let current_list = get_current_list(config.clone())?; - - mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; + mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?; Ok(()) } -pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> Result<(), Box> { +pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> MLE<()> { println!("Adding mod(s) {:?}", mod_id); let projects = if mod_id.len() == 1 { @@ -50,7 +41,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl current_version_id = current_version.clone().unwrap().id; - file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; + file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; for ver in available_versions { available_versions_vec.push(ver.id); @@ -67,7 +58,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl match userlist_get_all_ids(config.clone(), list.clone().id) { Ok(mods) => { if mods.contains(&project.id) { - return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } + return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } else { userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; } @@ -88,24 +79,22 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl }, }; - if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; + if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; } Ok(()) } -fn remove(config: Cfg, args: Vec) -> Result<(), Box> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; +fn remove(config: Cfg, input: Input) -> MLE<()> { - let current_list = get_current_list(config.clone())?; - let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?; + let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; - let version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; + let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; //TODO implement remove from modlist if not in any other lists && config clean is true - userlist_remove(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; - delete_version(current_list, version)?; + userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; + delete_version(input.list.unwrap(), version)?; let list_ids = lists_get_all_ids(config.clone())?; diff --git a/src/commands/setup.rs b/src/commands/setup.rs index e4fa801..cc7472c 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -63,4 +63,4 @@ fn to_04(config: Cfg) -> Result<(), Box> { s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?; } s_config_update_version(config, String::from("0.4")) -} +} \ No newline at end of file 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