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.rs87
1 files changed, 47 insertions, 40 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index ca28130..d400a24 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,9 +1,6 @@
1use std::io::{Error, ErrorKind}; 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}};
2
3use 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}};
4 2
5pub async fn update(config: Cfg, input: Input) -> MLE<()> { 3pub async fn update(config: Cfg, input: Input) -> MLE<()> {
6
7 let mut liststack: Vec<List> = vec![]; 4 let mut liststack: Vec<List> = vec![];
8 if input.all_lists { 5 if input.all_lists {
9 let list_ids = lists_get_all_ids(config.clone())?; 6 let list_ids = lists_get_all_ids(config.clone())?;
@@ -12,10 +9,9 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> {
12 } 9 }
13 } else { 10 } else {
14 let current = get_current_list(config.clone())?; 11 let current = get_current_list(config.clone())?;
15 println!("Checking for updates of mods in {}", current.id); 12 println!("Check for updates of mods in list {}", current.id);
16 liststack.push(current) 13 liststack.push(current)
17 } 14 }
18
19 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} 16}
21 17
@@ -31,6 +27,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
31 let mut projects = projects(String::from(&config.apis.modrinth), mods).await; 27 let mut projects = projects(String::from(&config.apis.modrinth), mods).await;
32 projects.sort_by_key(|pro| pro.id.clone()); 28 projects.sort_by_key(|pro| pro.id.clone());
33 29
30 println!("Comparing mod versions:");
34 let mut updatestack: Vec<Version> = vec![]; 31 let mut updatestack: Vec<Version> = vec![];
35 for (index, project) in projects.into_iter().enumerate() { 32 for (index, project) in projects.into_iter().enumerate() {
36 //Get versions for project and check if they match up 33 //Get versions for project and check if they match up
@@ -39,6 +36,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
39 let v_id = &current_version.mod_id; 36 let v_id = &current_version.mod_id;
40 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) }; 37 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) };
41 38
39 println!("\t({}) Check for update", project.title);
40
42 //Getting current installed version for disable or delete 41 //Getting current installed version for disable or delete
43 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?; 42 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?;
44 43
@@ -51,51 +50,52 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
51 current_versions.push((disable_version, p_id)); 50 current_versions.push((disable_version, p_id));
52 ver 51 ver
53 }, 52 },
54 //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") 53 Err(e) => {
55 Err(..) => { 54 //Catch no update available
56 //Updating versions in modlist for no repeating version calls 55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
57 mods_change_versions(config.clone(), version_db_string, project.id)?; 56 mods_change_versions(config.clone(), version_db_string, project.id)?;
58 println!("({}) No new version found for the specified", project.title); 57 println!("\t └No new version found for the specified minecraft version");
58 } else {
59 return Err(e);
60 };
59 continue; 61 continue;
60 }, 62 },
61 }); 63 });
62 } else { 64 } else {
63 println!("({}) No new version found", project.title); 65 println!("\t No new version found");
64 }; 66 };
65 }; 67 };
68
69 //Linebreak readability
70 println!("");
66 71
67 if clean { 72 if clean { clean_list_dir(&current_list)? };
68 let dl_path = &current_list.download_folder;
69 println!("Cleaning {}", dl_path);
70 for entry in std::fs::read_dir(dl_path)? {
71 let entry = entry?;
72 std::fs::remove_file(entry.path())?;
73 }
74 }
75 73
76 if direct_download { 74 //Linebreak readability
77 download_versions(current_list.clone(), updatestack).await?; 75 println!("");
76
77 if direct_download && !updatestack.is_empty() {
78 download_versions(current_list.clone(), config.clone(), updatestack).await?;
78 79
79 //Disable old versions 80 //Disable old versions
80 for ver in current_versions { 81 if !clean {
81 if delete_old { 82 for ver in current_versions {
82 println!("Deleting version {} for mod {}", ver.0, ver.1); 83 if delete_old {
83 delete_version(current_list.clone(), ver.0)?; 84 println!("Deleting version {} for mod {}", ver.0, ver.1);
84 } else if ver.0 != "NONE" { 85 delete_version(current_list.clone(), ver.0)?;
85 println!("Disabling version {} for mod {}", ver.0, ver.1); 86 } else if ver.0 != "NONE" {
86 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; 87 println!("Disabling version {} for mod {}", ver.0, ver.1);
87 }; 88 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
89 };
90 }
88 } 91 }
89 }; 92 };
90
91 } 93 }
92 94
93 Ok(()) 95 Ok(())
94} 96}
95 97
96async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> { 98async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> {
97 println!("Checking update for '{}' in {}", project.title, list.id);
98
99 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; 99 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;
100 100
101 let mut versions: Vec<String> = vec![]; 101 let mut versions: Vec<String> = vec![];
@@ -112,18 +112,24 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project)
112 let mut current: Vec<Version> = vec![]; 112 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))?) { 113 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) {
114 //get new versions 114 //get new versions
115 print!(" | getting new version"); 115 println!("\t └Get versions for specified minecraft versions");
116 let current_str = extract_current_version(applicable_versions.clone())?; 116 let current_str = extract_current_version(applicable_versions.clone())?;
117 let current_ver = applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("")?; 117 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),
119 Err(e) => Err(MLError::new(ErrorType::Other, e)),
120 }?;
118 current.push(current_ver.clone()); 121 current.push(current_ver.clone());
119 122
120 let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url; 123 let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") {
124 Ok(p) => Ok(p),
125 Err(e) => Err(MLError::new(ErrorType::Other, e)),
126 }?.url;
121 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; 127 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?;
122 } 128 }
123 129
124 if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; 130 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) };
125 131
126 println!(" | ✔️"); 132 //println!(" ������️");
127 Ok(current[0].clone()) 133 Ok(current[0].clone())
128} 134}
129 135
@@ -132,6 +138,7 @@ async fn download_updates_test() {
132 138
133 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; 139 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List};
134 140
141 let config = Cfg::init("modlist.toml").unwrap();
135 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; 142 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") };
136 143
137 let versions = vec![Version { 144 let versions = vec![Version {
@@ -164,5 +171,5 @@ async fn download_updates_test() {
164 "fabric".to_string() 171 "fabric".to_string()
165 ] 172 ]
166 }]; 173 }];
167 assert!(download_versions(current_list, versions).await.is_ok()) 174 assert!(download_versions(current_list, config, versions).await.is_ok())
168} 175}