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.rs116
1 files changed, 86 insertions, 30 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index e5751c0..3d9578b 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,14 +1,30 @@
1use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, userlist_get_current_version, userlist_get_set_version, mods_get_info}, List, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; 1use crate::{
2 2 config::Cfg,
3pub async fn update(config: Cfg, liststack: Vec<List>, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { 3 db::{
4 mods_get_info, userlist_change_versions, userlist_get_all_ids,
5 userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version,
6 },
7 error::{ErrorType, MLError, MLE},
8 files::{clean_list_dir, delete_version, disable_version, download_versions},
9 modrinth::{extract_current_version, versions, Version},
10 List,
11};
12
13pub async fn update(
14 config: Cfg,
15 liststack: Vec<List>,
16 clean: bool,
17 direct_download: bool,
18 delete_old: bool,
19) -> MLE<()> {
4 for current_list in liststack { 20 for current_list in liststack {
5 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 21 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
6 22
7 let mut current_versions: Vec<(String, String)> = vec![]; 23 let mut current_versions: Vec<(String, String)> = vec![];
8 24
9 println!(" └Update mods:"); 25 println!(" └Update mods:");
10 let mut updatestack: Vec<Version> = vec![]; 26 let mut updatestack: Vec<Version> = vec![];
11 27
12 for id in mods { 28 for id in mods {
13 let info = mods_get_info(config.clone(), &id)?; 29 let info = mods_get_info(config.clone(), &id)?;
14 println!("\t└{}", info.title); 30 println!("\t└{}", info.title);
@@ -19,27 +35,39 @@ pub async fn update(config: Cfg, liststack: Vec<List>, clean: bool, direct_downl
19 } 35 }
20 36
21 //Getting current installed version for disable or delete 37 //Getting current installed version for disable or delete
22 let disable_version = userlist_get_current_version(config.clone(), &current_list.id, &id)?; 38 let disable_version =
39 userlist_get_current_version(config.clone(), &current_list.id, &id)?;
23 40
24 updatestack.push( 41 updatestack.push(
25 match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await { 42 match specific_update(
43 config.clone(),
44 clean,
45 current_list.clone(),
46 String::from(&id),
47 )
48 .await
49 {
26 Ok(ver) => { 50 Ok(ver) => {
27 current_versions.push((disable_version, id)); 51 current_versions.push((disable_version, id));
28 ver 52 ver
29 }, 53 }
30 Err(e) => { 54 Err(e) => {
31 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { 55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
32 println!("\t └No new version found for the specified minecraft version"); 56 println!(
57 "\t └No new version found for the specified minecraft version"
58 );
33 } else { 59 } else {
34 return Err(e); 60 return Err(e);
35 }; 61 };
36 continue; 62 continue;
37 } 63 }
38 } 64 },
39 ) 65 )
40 }; 66 }
41 67
42 if clean { clean_list_dir(&current_list)?; }; 68 if clean {
69 clean_list_dir(&current_list)?;
70 };
43 71
44 if direct_download && !updatestack.is_empty() { 72 if direct_download && !updatestack.is_empty() {
45 download_versions(current_list.clone(), config.clone(), updatestack).await?; 73 download_versions(current_list.clone(), config.clone(), updatestack).await?;
@@ -50,7 +78,7 @@ pub async fn update(config: Cfg, liststack: Vec<List>, clean: bool, direct_downl
50 if delete_old { 78 if delete_old {
51 println!("\t └Delete version {}", ver.0); 79 println!("\t └Delete version {}", ver.0);
52 delete_version(current_list.clone(), ver.0)?; 80 delete_version(current_list.clone(), ver.0)?;
53 } else if ver.0 != "NONE" { 81 } else if ver.0 != "NONE" {
54 println!("\t └Disable version {}", ver.0); 82 println!("\t └Disable version {}", ver.0);
55 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; 83 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
56 }; 84 };
@@ -63,10 +91,11 @@ pub async fn update(config: Cfg, liststack: Vec<List>, clean: bool, direct_downl
63} 91}
64 92
65async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> { 93async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> {
66 let applicable_versions = versions(&config.apis.modrinth, String::from(&id), list.clone()).await; 94 let applicable_versions =
67 95 versions(&config.apis.modrinth, String::from(&id), list.clone()).await;
96
68 let mut versions: Vec<String> = vec![]; 97 let mut versions: Vec<String> = vec![];
69 98
70 if !applicable_versions.is_empty() { 99 if !applicable_versions.is_empty() {
71 for ver in &applicable_versions { 100 for ver in &applicable_versions {
72 versions.push(String::from(&ver.id)); 101 versions.push(String::from(&ver.id));
@@ -77,8 +106,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
77 106
78 let mut current: Vec<Version> = vec![]; 107 let mut current: Vec<Version> = vec![];
79 //TODO Split clean and no match 108 //TODO Split clean and no match
80 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&id))?) { 109 if clean
81 110 || (versions.join("|")
111 != userlist_get_applicable_versions(
112 config.clone(),
113 String::from(&list.id),
114 String::from(&id),
115 )?)
116 {
82 let current_str = extract_current_version(applicable_versions.clone())?; 117 let current_str = extract_current_version(applicable_versions.clone())?;
83 118
84 if clean { 119 if clean {
@@ -89,35 +124,54 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
89 }; 124 };
90 125
91 //get new versions 126 //get new versions
92 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { 127 let current_ver = match applicable_versions
128 .into_iter()
129 .find(|ver| ver.id == current_str)
130 .ok_or("!no current version in applicable_versions")
131 {
93 Ok(v) => Ok(v), 132 Ok(v) => Ok(v),
94 Err(e) => Err(MLError::new(ErrorType::Other, e)), 133 Err(e) => Err(MLError::new(ErrorType::Other, e)),
95 }?; 134 }?;
96 current.push(current_ver.clone()); 135 current.push(current_ver.clone());
97 136
98 //TODO implement version selection if no primary 137 //TODO implement version selection if no primary
99 let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { 138 let link = match current_ver
139 .files
140 .into_iter()
141 .find(|f| f.primary)
142 .ok_or("!no primary in links")
143 {
100 Ok(p) => Ok(p), 144 Ok(p) => Ok(p),
101 Err(e) => Err(MLError::new(ErrorType::Other, e)), 145 Err(e) => Err(MLError::new(ErrorType::Other, e)),
102 }?.url; 146 }?
147 .url;
103 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; 148 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?;
104 } 149 }
105 150
106 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; 151 if current.is_empty() {
107 152 return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE"));
153 };
154
108 //println!(" └✔️"); 155 //println!(" └✔️");
109 Ok(current[0].clone()) 156 Ok(current[0].clone())
110} 157}
111 158
112#[tokio::test] 159#[tokio::test]
113async fn download_updates_test() { 160async fn download_updates_test() {
161 use crate::{
162 modrinth::{Hash, Version, VersionFile, VersionType},
163 List, Modloader,
164 };
114 165
115 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List};
116
117 let config = Cfg::init("modlist.toml").unwrap(); 166 let config = Cfg::init("modlist.toml").unwrap();
118 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Fabric, download_folder: String::from("./dev/tests/dl") }; 167 let current_list = List {
119 168 id: String::from("..."),
120 let versions = vec![Version { 169 mc_version: String::from("..."),
170 modloader: Modloader::Fabric,
171 download_folder: String::from("./dev/tests/dl"),
172 };
173
174 let versions = vec![Version {
121 id: "dEqtGnT9".to_string(), 175 id: "dEqtGnT9".to_string(),
122 project_id: "kYuIpRLv".to_string(), 176 project_id: "kYuIpRLv".to_string(),
123 author_id: "Qnt13hO8".to_string(), 177 author_id: "Qnt13hO8".to_string(),
@@ -147,5 +201,7 @@ async fn download_updates_test() {
147 "fabric".to_string() 201 "fabric".to_string()
148 ] 202 ]
149 }]; 203 }];
150 assert!(download_versions(current_list, config, versions).await.is_ok()) 204 assert!(download_versions(current_list, config, versions)
205 .await
206 .is_ok())
151} 207}