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.rs160
1 files changed, 73 insertions, 87 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 721ced5..92ea9d6 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,13 +1,19 @@
1use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 1use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
2 2
3use crate::{ 3use crate::{
4 apis::modrinth::{extract_current_version, versions, Version}, config::Cfg, data::list::List, db::{ 4 apis::modrinth::{extract_current_version, versions, Version},
5 config::Cfg,
6 data::list::List,
7 db::{
5 mods_get_info, userlist_change_versions, userlist_get_all_ids, 8 mods_get_info, userlist_change_versions, userlist_get_all_ids,
6 userlist_get_applicable_versions, userlist_get_current_version, 9 userlist_get_applicable_versions, userlist_get_current_version,
7 userlist_get_set_version, 10 userlist_get_set_version,
8 }, error::{EType, MLErr, MLE}, files::{ 11 },
12 errors::{Error, MLE},
13 files::{
9 clean_list_dir, delete_version, disable_version, download_versions, 14 clean_list_dir, delete_version, disable_version, download_versions,
10 }, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION 15 },
16 PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION,
11}; 17};
12 18
13/// # Errors 19/// # Errors
@@ -20,15 +26,9 @@ pub async fn update(
20) -> MLE<()> { 26) -> MLE<()> {
21 let mp = MultiProgress::new(); 27 let mp = MultiProgress::new();
22 28
23 let update_p = mp.add(ProgressBar::new( 29 let update_p = mp.add(ProgressBar::new(liststack.len().try_into()?));
24 liststack
25 .len()
26 .try_into()
27 .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?,
28 ));
29 update_p.set_style( 30 update_p.set_style(
30 ProgressStyle::with_template(STYLE_BAR_POS) 31 ProgressStyle::with_template(STYLE_BAR_POS)?
31 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
32 .progress_chars(PROGRESS_CHARS), 32 .progress_chars(PROGRESS_CHARS),
33 ); 33 );
34 34
@@ -36,26 +36,15 @@ pub async fn update(
36 update_p.set_message(format!("Update {}", current_list.id)); 36 update_p.set_message(format!("Update {}", current_list.id));
37 37
38 let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); 38 let list_p = mp.insert_before(&update_p, ProgressBar::new(2));
39 list_p.set_style( 39 list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?);
40 ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
41 MLErr::new(EType::LibIndicatif, "template error")
42 })?,
43 );
44 list_p.set_message("Update mods"); 40 list_p.set_message("Update mods");
45 41
46 let mods = userlist_get_all_ids(config, &current_list.id)?; 42 let mods = userlist_get_all_ids(config, &current_list.id)?;
47 43
48 let list_u_p = mp.insert_before( 44 let list_u_p =
49 &list_p, 45 mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into()?));
50 ProgressBar::new(
51 mods.len()
52 .try_into()
53 .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?,
54 ),
55 );
56 list_u_p.set_style( 46 list_u_p.set_style(
57 ProgressStyle::with_template(STYLE_BAR_POS) 47 ProgressStyle::with_template(STYLE_BAR_POS)?
58 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
59 .progress_chars(PROGRESS_CHARS), 48 .progress_chars(PROGRESS_CHARS),
60 ); 49 );
61 50
@@ -63,7 +52,16 @@ pub async fn update(
63 let mut updatestack: Vec<Version> = vec![]; 52 let mut updatestack: Vec<Version> = vec![];
64 53
65 for id in mods { 54 for id in mods {
66 update_mod(config, id, list_u_p.clone(), &current_list, &mut updatestack, &mut current_versions, clean).await?; 55 update_mod(
56 config,
57 id,
58 list_u_p.clone(),
59 &current_list,
60 &mut updatestack,
61 &mut current_versions,
62 clean,
63 )
64 .await?;
67 } 65 }
68 66
69 list_u_p.finish_with_message(format!( 67 list_u_p.finish_with_message(format!(
@@ -91,12 +89,11 @@ pub async fn update(
91 let d_p = mp.insert_before( 89 let d_p = mp.insert_before(
92 &list_p, 90 &list_p,
93 ProgressBar::new( 91 ProgressBar::new(
94 current_versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, 92 current_versions.len().try_into()?,
95 ), 93 ),
96 ); 94 );
97 d_p.set_style( 95 d_p.set_style(
98 ProgressStyle::with_template(STYLE_BAR_POS) 96 ProgressStyle::with_template(STYLE_BAR_POS)?
99 .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
100 .progress_chars(PROGRESS_CHARS), 97 .progress_chars(PROGRESS_CHARS),
101 ); 98 );
102 for ver in current_versions { 99 for ver in current_versions {
@@ -129,45 +126,40 @@ pub async fn update(
129 Ok(()) 126 Ok(())
130} 127}
131 128
132async fn update_mod(config: &Cfg, id: String, list_u_p: ProgressBar, current_list: &List, updatestack: &mut Vec<Version>, current_versions: &mut Vec<(String, String)>, clean: bool) -> MLE<()> { 129async fn update_mod(
133 let info = mods_get_info(config, &id)?; 130 config: &Cfg,
134 list_u_p.set_message(format!("Update {}", info.title)); 131 id: String,
132 list_u_p: ProgressBar,
133 current_list: &List,
134 updatestack: &mut Vec<Version>,
135 current_versions: &mut Vec<(String, String)>,
136 clean: bool,
137) -> MLE<()> {
138 let info = mods_get_info(config, &id)?;
139 list_u_p.set_message(format!("Update {}", info.title));
135 140
136 //Skip check if version is set 141 //Skip check if version is set
137 if userlist_get_set_version(config, &current_list.id, &id)? { 142 if userlist_get_set_version(config, &current_list.id, &id)? {
138 list_u_p.inc(1); 143 list_u_p.inc(1);
139 return Ok(()); 144 return Ok(());
140 } 145 }
141 146
142 //Getting current installed version for disable or delete 147 //Getting current installed version for disable or delete
143 let disable_version = 148 let disable_version =
144 userlist_get_current_version(config, &current_list.id, &id)?; 149 userlist_get_current_version(config, &current_list.id, &id)?;
145 150
146 updatestack.push( 151 let version = specific_update(
147 match specific_update( 152 config,
148 config, 153 clean,
149 clean, 154 current_list.clone(),
150 current_list.clone(), 155 &id,
151 &id, 156 &list_u_p,
152 &list_u_p, 157 ).await?;
153 ) 158 if let Some(v) = version {
154 .await 159 updatestack.push(v);
155 { 160 current_versions.push((disable_version, id.to_string()));
156 Ok(ver) => { 161 }
157 current_versions.push((disable_version, id.to_string())); 162 list_u_p.inc(1);
158 ver
159 }
160 Err(e) => {
161 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
162 } else {
163 return Err(e);
164 };
165 list_u_p.inc(1);
166 return Ok(());
167 }
168 },
169 );
170 list_u_p.inc(1);
171 163
172 Ok(()) 164 Ok(())
173} 165}
@@ -178,7 +170,7 @@ async fn specific_update(
178 list: List, 170 list: List,
179 id: &str, 171 id: &str,
180 progress: &ProgressBar, 172 progress: &ProgressBar,
181) -> MLE<Version> { 173) -> MLE<Option<Version>> {
182 let applicable_versions = 174 let applicable_versions =
183 versions(&config.apis.modrinth, String::from(id), list.clone()).await?; 175 versions(&config.apis.modrinth, String::from(id), list.clone()).await?;
184 176
@@ -192,14 +184,12 @@ async fn specific_update(
192 } 184 }
193 } 185 }
194 186
195 let mut current: Vec<Version> = vec![]; 187 let mut current: Option<Version> = None;
196 if clean 188 if clean || (versions.join("|") != userlist_get_applicable_versions(
197 || (versions.join("|")
198 != userlist_get_applicable_versions(
199 config, 189 config,
200 &list.id, 190 &list.id,
201 String::from(id), 191 String::from(id),
202 )?) 192 )?)
203 { 193 {
204 let current_str = extract_current_version(applicable_versions.clone())?; 194 let current_str = extract_current_version(applicable_versions.clone())?;
205 195
@@ -211,15 +201,10 @@ async fn specific_update(
211 } 201 }
212 202
213 //get new versions 203 //get new versions
214 let current_ver = match applicable_versions 204 let Some(current_ver) = applicable_versions.into_iter().find(|ver| ver.id == current_str) else {
215 .into_iter() 205 return Err(Error::NoCurrentVersion);
216 .find(|ver| ver.id == current_str) 206 };
217 .ok_or("!no current version in applicable_versions") 207 current = Some(current_ver.clone());
218 {
219 Ok(v) => Ok(v),
220 Err(e) => Err(MLErr::new(EType::Other, e)),
221 }?;
222 current.push(current_ver.clone());
223 208
224 let files = &current_ver.files; 209 let files = &current_ver.files;
225 210
@@ -237,11 +222,12 @@ async fn specific_update(
237 link, 222 link,
238 id.to_string(), 223 id.to_string(),
239 )?; 224 )?;
240 }
241
242 if current.is_empty() {
243 return Err(MLErr::new(EType::ModError, "NO_UPDATE_AVAILABLE"));
244 }; 225 };
245 226
246 Ok(current[0].clone()) 227 if current.is_none() {
228 // No Update Available
229 Ok(None)
230 } else {
231 Ok(current)
232 }
247} 233}