summaryrefslogtreecommitdiff
path: root/src/commands/update.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/update.rs')
-rw-r--r--src/commands/update.rs90
1 files changed, 40 insertions, 50 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index d400a24..bc5b316 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,4 +1,4 @@
1use 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}}; 1use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_get_title, userlist_get_set_version}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}};
2 2
3pub async fn update(config: Cfg, input: Input) -> MLE<()> { 3pub async fn update(config: Cfg, input: Input) -> MLE<()> {
4 let mut liststack: Vec<List> = vec![]; 4 let mut liststack: Vec<List> = vec![];
@@ -9,7 +9,7 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> {
9 } 9 }
10 } else { 10 } else {
11 let current = get_current_list(config.clone())?; 11 let current = get_current_list(config.clone())?;
12 println!("Check for updates of mods in list {}", current.id); 12 println!("Update list {}:", current.id);
13 liststack.push(current) 13 liststack.push(current)
14 } 14 }
15 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await 15 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await
@@ -20,59 +20,41 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
20 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 20 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
21 21
22 let mut current_versions: Vec<(String, String)> = vec![]; 22 let mut current_versions: Vec<(String, String)> = vec![];
23
24 println!(" └Update mods:");
25 let mut updatestack: Vec<Version> = vec![];
26
27 for id in mods {
28 let title = mods_get_title(config.clone(), &id)?;
29 println!("\t└{}", title);
23 30
24 let mut versions = mods_get_versions(config.clone(), mods.clone())?; 31 if userlist_get_set_version(config.clone(), &current_list.id, &id)? {
25 versions.sort_by_key(|ver| ver.mod_id.clone()); 32 println!("\t └Set version, skipping update");
26 33 continue;
27 let mut projects = projects(String::from(&config.apis.modrinth), mods).await; 34 }
28 projects.sort_by_key(|pro| pro.id.clone());
29 35
30 println!("Comparing mod versions:");
31 let mut updatestack: Vec<Version> = vec![];
32 for (index, project) in projects.into_iter().enumerate() {
33 //Get versions for project and check if they match up
34 let current_version = &versions[index];
35 let p_id = String::from(&project.id);
36 let v_id = &current_version.mod_id;
37 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) };
38
39 println!("\t({}) Check for update", project.title);
40
41 //Getting current installed version for disable or delete 36 //Getting current installed version for disable or delete
42 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?; 37 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&id))?;
43
44 let version_db_string = project.versions.join("|");
45 38
46 //Adding to stack if not the same versions in the list OR if clean == true 39 updatestack.push(
47 if clean || (version_db_string != current_version.versions) { 40 match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await {
48 updatestack.push(match specific_update(config.clone(), clean, current_list.clone(), project.clone()).await {
49 Ok(ver) => { 41 Ok(ver) => {
50 current_versions.push((disable_version, p_id)); 42 current_versions.push((disable_version, id));
51 ver 43 ver
52 }, 44 },
53 Err(e) => { 45 Err(e) => {
54 //Catch no update available
55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { 46 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
56 mods_change_versions(config.clone(), version_db_string, project.id)?; 47 println!("\t └No new version found for the specified minecraft version");
57 println!("\t └No new version found for the specified minecraft version");
58 } else { 48 } else {
59 return Err(e); 49 return Err(e);
60 }; 50 };
61 continue; 51 continue;
62 }, 52 }
63 }); 53 }
64 } else { 54 )
65 println!("\t └No new version found");
66 };
67 }; 55 };
68
69 //Linebreak readability
70 println!("");
71 56
72 if clean { clean_list_dir(&current_list)? }; 57 if clean { clean_list_dir(&current_list)?; };
73
74 //Linebreak readability
75 println!("");
76 58
77 if direct_download && !updatestack.is_empty() { 59 if direct_download && !updatestack.is_empty() {
78 download_versions(current_list.clone(), config.clone(), updatestack).await?; 60 download_versions(current_list.clone(), config.clone(), updatestack).await?;
@@ -81,10 +63,10 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
81 if !clean { 63 if !clean {
82 for ver in current_versions { 64 for ver in current_versions {
83 if delete_old { 65 if delete_old {
84 println!("Deleting version {} for mod {}", ver.0, ver.1); 66 println!("\t └Delete version {}", ver.0);
85 delete_version(current_list.clone(), ver.0)?; 67 delete_version(current_list.clone(), ver.0)?;
86 } else if ver.0 != "NONE" { 68 } else if ver.0 != "NONE" {
87 println!("Disabling version {} for mod {}", ver.0, ver.1); 69 println!("\t └Disable version {}", ver.0);
88 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; 70 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
89 }; 71 };
90 } 72 }
@@ -95,8 +77,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
95 Ok(()) 77 Ok(())
96} 78}
97 79
98async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> { 80async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> {
99 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; 81 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&id), list.clone()).await;
100 82
101 let mut versions: Vec<String> = vec![]; 83 let mut versions: Vec<String> = vec![];
102 84
@@ -108,12 +90,20 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project)
108 versions.push(String::from("NONE")); 90 versions.push(String::from("NONE"));
109 } 91 }
110 92
111
112 let mut current: Vec<Version> = vec![]; 93 let mut current: Vec<Version> = vec![];
113 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { 94 //TODO Split clean and no match
114 //get new versions 95 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&id))?) {
115 println!("\t └Get versions for specified minecraft versions"); 96
116 let current_str = extract_current_version(applicable_versions.clone())?; 97 let current_str = extract_current_version(applicable_versions.clone())?;
98
99 if clean {
100 println!("\t └Add version to downloadstack");
101 } else {
102 println!("\t └Get versions for specified minecraft versions");
103 println!("\t └New current version: {}", current_str);
104 };
105
106 //get new versions
117 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { 107 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") {
118 Ok(v) => Ok(v), 108 Ok(v) => Ok(v),
119 Err(e) => Err(MLError::new(ErrorType::Other, e)), 109 Err(e) => Err(MLError::new(ErrorType::Other, e)),
@@ -124,7 +114,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project)
124 Ok(p) => Ok(p), 114 Ok(p) => Ok(p),
125 Err(e) => Err(MLError::new(ErrorType::Other, e)), 115 Err(e) => Err(MLError::new(ErrorType::Other, e)),
126 }?.url; 116 }?.url;
127 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; 117 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?;
128 } 118 }
129 119
130 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; 120 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) };
@@ -139,7 +129,7 @@ async fn download_updates_test() {
139 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; 129 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List};
140 130
141 let config = Cfg::init("modlist.toml").unwrap(); 131 let config = Cfg::init("modlist.toml").unwrap();
142 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; 132 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Fabric, download_folder: String::from("./dev/tests/dl") };
143 133
144 let versions = vec![Version { 134 let versions = vec![Version {
145 id: "dEqtGnT9".to_string(), 135 id: "dEqtGnT9".to_string(),