From fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 17 Nov 2022 21:20:09 +0100 Subject: added --clean for update && list downloadfolder --- src/commands/update.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index e383eae..85630f5 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -4,9 +4,9 @@ use reqwest::Client; use futures_util::StreamExt; -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}, List}; +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}, List, input::Input}; -pub async fn update(config: Cfg) -> Result<(), Box> { +pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { let current_list = get_current_list(config.clone())?; @@ -20,15 +20,16 @@ pub async fn update(config: Cfg) -> Result<(), Box> { let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { + //Get versions for project and check if they match up let current_version = &versions[index]; - let p_id = String::from(&project.id); let v_id = ¤t_version.mod_id; - if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; - - if project.versions.join("|") != current_version.versions { - updatestack.push(match specific_update(config.clone(), current_list.clone(), project).await { + + + //Adding to stack if not the same versions in the list OR if clean == true + if input.clone().clean || (project.versions.join("|") != current_version.versions) { + updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project).await { Ok(ver) => ver, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") Err(_) => { continue; }, @@ -36,13 +37,23 @@ pub async fn update(config: Cfg) -> Result<(), Box> { }; }; //println!("{:?}", updatestack); + + + 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())?; + } + } - download_updates(config, updatestack).await?; + download_updates(config, current_list, updatestack).await?; Ok(()) } -async fn specific_update(config: Cfg, list: List, project: Project) -> Result> { +async fn specific_update(config: Cfg, input: Input, list: List, project: Project) -> Result> { print!("Checking update for '{}' in {}", project.title, list.id); let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; @@ -54,7 +65,7 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result = vec![]; - if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? { + if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { //get new versions print!(" | getting new version"); let current_str = extract_current_version(applicable_versions.clone())?; @@ -71,9 +82,9 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result) -> Result> { +async fn download_updates(config: Cfg, current_list: List, versions: Vec) -> Result> { - let dl_path = String::from(&config.downloads); + let dl_path = String::from(¤t_list.download_folder); for ver in versions { let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); -- cgit v1.2.3