summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs114
1 files changed, 57 insertions, 57 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 8abf913..fdb70c7 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,23 +1,23 @@
1use std::{io::Write, collections::HashMap}; 1use std::collections::HashMap;
2 2
3use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; 3use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
4 4
5use crate::{ 5use crate::{
6 config::Cfg, 6 config::Cfg,
7 db::{ 7 db::{
8 lists_get_all_ids, mods_get_id, mods_insert, mods_remove, userlist_get_all_ids, 8 lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, mods_remove,
9 userlist_get_current_version, userlist_insert, userlist_remove, mods_get_info, 9 userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove,
10 }, 10 },
11 error::{ErrorType, MLError, MLE}, 11 error::{ErrorType, MLError, MLE},
12 files::{delete_version, download_versions}, 12 files::{delete_version, download_versions},
13 modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, 13 modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version},
14 List, PROGRESS_CHARS, STYLE_BAR_POS, 14 List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION,
15}; 15};
16 16
17#[derive(Debug)] 17#[derive(Debug)]
18pub struct AddMod { 18pub struct AddMod {
19 pub id: IDSelector, 19 pub id: IDSelector,
20 pub set_version: bool 20 pub set_version: bool,
21} 21}
22 22
23#[derive(Debug, PartialEq, Eq)] 23#[derive(Debug, PartialEq, Eq)]
@@ -47,9 +47,13 @@ pub async fn mod_add(
47 47
48 let mut mod_ids: Vec<(String, bool)> = Vec::new(); 48 let mut mod_ids: Vec<(String, bool)> = Vec::new();
49 let mut ver_ids: Vec<(String, bool)> = Vec::new(); 49 let mut ver_ids: Vec<(String, bool)> = Vec::new();
50 50
51 let add_p = mp.add(ProgressBar::new(mods.len().try_into().unwrap())); 51 let add_p = mp.add(ProgressBar::new(mods.len().try_into().unwrap()));
52 add_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 52 add_p.set_style(
53 ProgressStyle::with_template(STYLE_BAR_POS)
54 .unwrap()
55 .progress_chars(PROGRESS_CHARS),
56 );
53 add_p.set_message("Sort ids"); 57 add_p.set_message("Sort ids");
54 58
55 //"Sort" project ids from version ids to be able to handle them differently but in a batch 59 //"Sort" project ids from version ids to be able to handle them differently but in a batch
@@ -62,7 +66,7 @@ pub async fn mod_add(
62 } 66 }
63 67
64 add_p.set_message("Get infos"); 68 add_p.set_message("Get infos");
65 69
66 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 70 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
67 if !mod_ids.is_empty() { 71 if !mod_ids.is_empty() {
68 projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); 72 projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?);
@@ -80,11 +84,17 @@ pub async fn mod_add(
80 let mut downloadstack: Vec<Version> = Vec::new(); 84 let mut downloadstack: Vec<Version> = Vec::new();
81 85
82 //Adding each mod to the lists and downloadstack 86 //Adding each mod to the lists and downloadstack
83 let project_p = mp.insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into().unwrap())); 87 let project_p = mp.insert_before(
84 project_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 88 &add_p,
89 ProgressBar::new(projectinfo.len().try_into().unwrap()),
90 );
91 project_p.set_style(
92 ProgressStyle::with_template(STYLE_BAR_POS)
93 .unwrap()
94 .progress_chars(PROGRESS_CHARS),
95 );
85 96
86 for project in projectinfo { 97 for project in projectinfo {
87
88 project_p.set_message(format!("Add {}", project.title)); 98 project_p.set_message(format!("Add {}", project.title));
89 99
90 let current_version_id = if project.current_version.is_none() { 100 let current_version_id = if project.current_version.is_none() {
@@ -116,12 +126,7 @@ pub async fn mod_add(
116 Ok(..) => Ok(..), 126 Ok(..) => Ok(..),
117 }?; 127 }?;
118 128
119 match mods_insert( 129 match mods_insert(config, &project.mod_id, &project.slug, &project.title) {
120 config,
121 &project.mod_id,
122 &project.slug,
123 &project.title,
124 ) {
125 Err(e) => { 130 Err(e) => {
126 if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { 131 if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" {
127 Ok(..) 132 Ok(..)
@@ -152,8 +157,11 @@ pub async fn mod_add(
152 Ok(()) 157 Ok(())
153} 158}
154 159
155async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE<Vec<ProjectInfo>> { 160async fn get_mod_infos(
156 161 config: &Cfg,
162 mod_ids: Vec<(String, bool)>,
163 list: List,
164) -> MLE<Vec<ProjectInfo>> {
157 let mut setmap: HashMap<String, bool> = HashMap::new(); 165 let mut setmap: HashMap<String, bool> = HashMap::new();
158 166
159 let mut ids = vec![]; 167 let mut ids = vec![];
@@ -197,19 +205,15 @@ async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) -
197 .find(|v| v.id == current_id) 205 .find(|v| v.id == current_id)
198 .unwrap(), 206 .unwrap(),
199 ); 207 );
200 208
201 // match primary, if none? 209 // match primary, if none?
202 let files = current_version 210 let files = current_version.clone().ok_or("").unwrap().files;
203 .clone()
204 .ok_or("")
205 .unwrap()
206 .files;
207 211
208 file = match files.clone().into_iter().find(|f| f.primary) { 212 file = match files.clone().into_iter().find(|f| f.primary) {
209 Some(f) => f, 213 Some(f) => f,
210 None => { files[0].clone() } 214 None => files[0].clone(),
211 } 215 }
212 .url; 216 .url;
213 217
214 for ver in available_versions { 218 for ver in available_versions {
215 available_versions_vec.push(ver.id); 219 available_versions_vec.push(ver.id);
@@ -247,7 +251,6 @@ async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) -
247} 251}
248 252
249async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<ProjectInfo>> { 253async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<ProjectInfo>> {
250
251 let mut setmap: HashMap<String, bool> = HashMap::new(); 254 let mut setmap: HashMap<String, bool> = HashMap::new();
252 255
253 let mut ids = vec![]; 256 let mut ids = vec![];
@@ -271,15 +274,13 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Pro
271 for (i, project) in v_projects.into_iter().enumerate() { 274 for (i, project) in v_projects.into_iter().enumerate() {
272 let version = &v_versions[i]; 275 let version = &v_versions[i];
273 276
274 let files = version 277 let files = version.clone().files;
275 .clone()
276 .files;
277 278
278 let file = match files.clone().into_iter().find(|f| f.primary) { 279 let file = match files.clone().into_iter().find(|f| f.primary) {
279 Some(f) => f, 280 Some(f) => f,
280 None => { files[0].clone() } 281 None => files[0].clone(),
281 } 282 }
282 .url; 283 .url;
283 284
284 projectinfo.push(ProjectInfo { 285 projectinfo.push(ProjectInfo {
285 mod_id: String::from(&project.id), 286 mod_id: String::from(&project.id),
@@ -300,34 +301,31 @@ async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<Pro
300/// * `config` - config struct 301/// * `config` - config struct
301/// * `id` - name, slug or id of the mod 302/// * `id` - name, slug or id of the mod
302/// * `list` - List struct 303/// * `list` - List struct
303pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> { 304pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> {
305 let progress = ProgressBar::new_spinner();
306 progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap());
307
304 let mod_id = mods_get_id(&config.data, id)?; 308 let mod_id = mods_get_id(&config.data, id)?;
305 309
306 println!("Remove mod {} from {}", mods_get_info(config, &mod_id)?.title, list.id); 310 let info = mods_get_info(config, &mod_id)?;
311
312 progress.set_message(format!("Remove {} from {}", info.title, list.id));
313
307 let version = userlist_get_current_version(config, &list.id, &mod_id)?; 314 let version = userlist_get_current_version(config, &list.id, &mod_id)?;
308 315
309 print!(" └Remove from list");
310 //Force flush of stdout, else print! doesn't print instantly
311 std::io::stdout().flush()?;
312 userlist_remove(config, &list.id, &mod_id)?; 316 userlist_remove(config, &list.id, &mod_id)?;
313 println!(" ✓");
314 317
315 print!(" └Delete file"); 318 progress.set_message("Delete file");
316 //Force flush of stdout, else print! doesn't print instantly
317 std::io::stdout().flush()?;
318 match delete_version(list, version) { 319 match delete_version(list, version) {
319 Ok(_) => (), 320 Ok(_) => (),
320 Err(err) => { 321 Err(err) => {
321 if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { 322 if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" {
322 return Err(err); 323 return Err(err);
323 }; 324 };
324 }, 325 }
325 }; 326 };
326 println!(" ✓");
327 327
328 print!(" └Clean main db table"); 328 progress.set_message("Check main list");
329 //Force flush of stdout, else print! doesn't print instantly
330 std::io::stdout().flush()?;
331 let list_ids = lists_get_all_ids(config)?; 329 let list_ids = lists_get_all_ids(config)?;
332 330
333 // Remove mod from main list if not used elsewhere 331 // Remove mod from main list if not used elsewhere
@@ -336,11 +334,11 @@ pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> {
336 let mods = match userlist_get_all_ids(config, &id) { 334 let mods = match userlist_get_all_ids(config, &id) {
337 Ok(m) => m, 335 Ok(m) => m,
338 Err(err) => { 336 Err(err) => {
339 if err.to_string() == "Database: NO_MODS_USERLIST" { 337 if err.to_string() == "Database: NO_MODS_USERLIST" {
340 println!(" ✓"); 338 println!(" ✓");
341 return Ok(()); 339 return Ok(());
342 }; 340 };
343 return Err(err) 341 return Err(err);
344 } 342 }
345 }; 343 };
346 if mods.contains(&mod_id) { 344 if mods.contains(&mod_id) {
@@ -350,9 +348,11 @@ pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> {
350 } 348 }
351 349
352 if !mod_used { 350 if !mod_used {
353 mods_remove(config, mod_id)?; 351 progress.set_message("Remove from main list");
352 mods_remove(config, &mod_id)?;
354 }; 353 };
355 println!(" ✓"); 354
355 progress.finish_with_message(format!("Removed {} from {}", info.title, list.id));
356 356
357 Ok(()) 357 Ok(())
358} 358}