diff options
author | fx <[email protected]> | 2023-05-29 18:02:08 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-05-29 18:02:08 +0200 |
commit | d3870a2efa74e68c643dfb4aef32edc2536503b0 (patch) | |
tree | 116075aaa57c35afca2749719d450c3cb473ab3e /src/commands/download.rs | |
parent | 5a2ea0755b29a8811aeeec1c73679c5783082628 (diff) | |
parent | c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231 (diff) | |
download | modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar.gz modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.zip |
Merge pull request 'multithreaded' (#6) from multithreaded into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/6
Diffstat (limited to 'src/commands/download.rs')
-rw-r--r-- | src/commands/download.rs | 93 |
1 files changed, 76 insertions, 17 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index ebfb4eb..a7cf744 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -1,26 +1,48 @@ | |||
1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | ||
2 | |||
1 | use crate::{config::Cfg, List}; | 3 | use crate::{config::Cfg, List}; |
2 | use crate::{ | 4 | use crate::{ |
3 | db::userlist_get_all_current_versions_with_mods, | 5 | db::userlist_get_all_current_versions_with_mods, |
4 | error::{ErrorType, MLError, MLE}, | 6 | error::{ErrorType, MLError, MLE}, |
5 | files::{ | 7 | files::{ |
6 | clean_list_dir, delete_version, disable_version, download_versions, get_downloaded_versions, | 8 | clean_list_dir, delete_version, disable_version, download_versions, |
9 | get_downloaded_versions, | ||
7 | }, | 10 | }, |
8 | modrinth::get_raw_versions, | 11 | modrinth::get_raw_versions, |
9 | }; | 12 | }; |
13 | use crate::{PROGRESS_CHARS, STYLE_BAR_POS}; | ||
10 | 14 | ||
11 | pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old: bool) -> MLE<()> { | 15 | pub async fn download( |
16 | config: &Cfg, | ||
17 | liststack: Vec<List>, | ||
18 | clean: bool, | ||
19 | delete_old: bool, | ||
20 | ) -> MLE<()> { | ||
21 | let mp = MultiProgress::new(); | ||
22 | let download_p = | ||
23 | mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); | ||
24 | download_p.set_style( | ||
25 | ProgressStyle::with_template(STYLE_BAR_POS) | ||
26 | .unwrap() | ||
27 | .progress_chars(PROGRESS_CHARS), | ||
28 | ); | ||
12 | 29 | ||
13 | for current_list in liststack { | 30 | for current_list in liststack { |
14 | println!("Downloading current versions of mods in {}", current_list.id); | 31 | download_p.set_message(format!("Download in {}", current_list.id)); |
15 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; | 32 | |
16 | // println!("To download: {:#?}", downloaded_versions); | 33 | let downloaded_versions = |
17 | let current_version_ids = match userlist_get_all_current_versions_with_mods( | 34 | get_downloaded_versions(current_list.clone())?; |
18 | config.clone(), | 35 | let current_version_ids = |
19 | String::from(¤t_list.id), | 36 | match userlist_get_all_current_versions_with_mods( |
20 | ) { | 37 | config, |
21 | Ok(i) => Ok(i), | 38 | String::from(¤t_list.id), |
22 | Err(e) => Err(MLError::new(ErrorType::DBError, e.to_string().as_str())), | 39 | ) { |
23 | }?; | 40 | Ok(i) => Ok(i), |
41 | Err(e) => Err(MLError::new( | ||
42 | ErrorType::DBError, | ||
43 | e.to_string().as_str(), | ||
44 | )), | ||
45 | }?; | ||
24 | 46 | ||
25 | let mut to_download: Vec<String> = vec![]; | 47 | let mut to_download: Vec<String> = vec![]; |
26 | //(mod_id, version_id) | 48 | //(mod_id, version_id) |
@@ -39,7 +61,10 @@ pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old | |||
39 | .ok_or("SOMETHING_HAS_REALLY_GONE_WRONG") | 61 | .ok_or("SOMETHING_HAS_REALLY_GONE_WRONG") |
40 | .unwrap(); | 62 | .unwrap(); |
41 | if ¤t_version != downloaded_version { | 63 | if ¤t_version != downloaded_version { |
42 | to_disable.push((mod_id.clone(), String::from(downloaded_version))); | 64 | to_disable.push(( |
65 | mod_id.clone(), | ||
66 | String::from(downloaded_version), | ||
67 | )); | ||
43 | to_download.push(current_version); | 68 | to_download.push(current_version); |
44 | } | 69 | } |
45 | } | 70 | } |
@@ -54,23 +79,57 @@ pub async fn download(config: Cfg, liststack: Vec<List>, clean: bool, delete_old | |||
54 | current_list.clone(), | 79 | current_list.clone(), |
55 | config.clone(), | 80 | config.clone(), |
56 | get_raw_versions(&config.apis.modrinth, to_download).await, | 81 | get_raw_versions(&config.apis.modrinth, to_download).await, |
82 | &mp, | ||
83 | &download_p, | ||
57 | ) | 84 | ) |
58 | .await?; | 85 | .await?; |
59 | } else { | 86 | } else { |
60 | println!("There are no new versions to download"); | 87 | download_p.println(format!( |
88 | "There are no new versions to download for {}", | ||
89 | current_list.id | ||
90 | )); | ||
61 | } | 91 | } |
62 | 92 | ||
63 | if !to_disable.is_empty() { | 93 | if !to_disable.is_empty() { |
94 | let d_p = mp.insert_before( | ||
95 | &download_p, | ||
96 | ProgressBar::new(to_disable.len().try_into().unwrap()), | ||
97 | ); | ||
98 | d_p.set_style( | ||
99 | ProgressStyle::with_template(STYLE_BAR_POS) | ||
100 | .unwrap() | ||
101 | .progress_chars(PROGRESS_CHARS), | ||
102 | ); | ||
64 | for ver in to_disable { | 103 | for ver in to_disable { |
65 | if delete_old { | 104 | if delete_old { |
66 | println!("Deleting version {} for mod {}", ver.1, ver.0); | 105 | d_p.set_message(format!("Delete version {}", ver.1)); |
67 | delete_version(current_list.clone(), ver.1)?; | 106 | d_p.inc(1); |
107 | delete_version(¤t_list, ver.1)?; | ||
68 | } else { | 108 | } else { |
69 | disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; | 109 | d_p.set_message(format!("Disable version {}", ver.1)); |
110 | d_p.inc(1); | ||
111 | disable_version( | ||
112 | config, | ||
113 | current_list.clone(), | ||
114 | ver.1, | ||
115 | ver.0, | ||
116 | )?; | ||
70 | }; | 117 | }; |
71 | } | 118 | } |
119 | |||
120 | let del_msg = if delete_old { | ||
121 | "Deleted all old versions" | ||
122 | } else { | ||
123 | "Disabled all old versions" | ||
124 | }; | ||
125 | |||
126 | d_p.finish_with_message(del_msg); | ||
72 | } | 127 | } |
128 | |||
129 | download_p.inc(1); | ||
73 | } | 130 | } |
74 | 131 | ||
132 | download_p.finish_with_message("Downloaded all lists"); | ||
133 | |||
75 | Ok(()) | 134 | Ok(()) |
76 | } | 135 | } |