From ddde9204c72dd867f920f07f6483be03dda7cf68 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 28 Nov 2022 22:55:14 +0100 Subject: basically update impl; added "good" download; auto dl on new mod; db to 0.4; etc --- src/commands/update.rs | 67 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index 42d19aa..c8f0880 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,9 +1,9 @@ -use std::io::{Error, ErrorKind}; +use std::{io::{Error, ErrorKind}, fs::{rename, remove_file}}; -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}, List, input::Input, download_file}; +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, userlist_add_disabled_versions, mods_change_versions}, List, input::Input, files::get_file_path, download_versions}; pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { - + let mut liststack: Vec = vec![]; if input.all_lists { let list_ids = lists_get_all_ids(config.clone())?; @@ -11,18 +11,22 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box = vec![]; let mut versions = mods_get_versions(config.clone(), mods.clone())?; versions.sort_by_key(|ver| ver.mod_id.clone()); let mut projects = projects(String::from(&config.apis.modrinth), mods).await; projects.sort_by_key(|pro| pro.id.clone()); - + let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { //Get versions for project and check if they match up @@ -30,14 +34,26 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box ver, + Ok(ver) => { + current_versions.push((disable_version, p_id)); + ver + }, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") - Err(_) => { println!("({}) No new version found for the specified minecraft version", project.title); continue; }, + Err(e) => { + //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 minecraft version({})", project.title, e); + continue; + }, }); } else { println!("({}) No new version found", project.title); @@ -53,14 +69,19 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box Result> { - print!("Checking update for '{}' in {}", project.title, list.id); + 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; @@ -88,17 +109,25 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project Ok(current[0].clone()) } -async fn download_updates(current_list: List, versions: Vec) -> Result> { +fn disable_old(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { + println!("Disabling version {} for mod {}", versionid, mod_id); + let file = get_file_path(current_list.clone(), String::from(&versionid))?; + let disabled = format!("{}.disabled", file); - let dl_path = String::from(¤t_list.download_folder); + rename(file, disabled)?; - for ver in versions { - let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); - download_file(primary_file.url, current_list.clone().download_folder, primary_file.filename).await?; - } + userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; + + Ok(()) +} +fn delete_old(current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { + println!("Deleting version {} for mod {}", versionid, mod_id); + let file = get_file_path(current_list, String::from(&versionid))?; + + remove_file(file)?; - Ok(dl_path) + Ok(()) } #[tokio::test] @@ -138,5 +167,5 @@ async fn download_updates_test() { "fabric".to_string() ] }]; - assert!(download_updates(current_list, versions).await.is_ok()) + assert!(download_versions(current_list, versions).await.is_ok()) } -- cgit v1.2.3