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