diff options
author | fxqnlr <[email protected]> | 2024-09-04 12:31:02 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-04 12:31:02 +0200 |
commit | 942558c75200aaad0b4d8561a1f6999f88f843a4 (patch) | |
tree | 8be4632c0677613c277403cb11d41cb6dff12b5b /src/commands/download.rs | |
parent | 29635b9e3833296b2c908914104ba7165d22d3d5 (diff) | |
download | modlist-942558c75200aaad0b4d8561a1f6999f88f843a4.tar modlist-942558c75200aaad0b4d8561a1f6999f88f843a4.tar.gz modlist-942558c75200aaad0b4d8561a1f6999f88f843a4.zip |
remove allow too_many_lines and fix them
Diffstat (limited to 'src/commands/download.rs')
-rw-r--r-- | src/commands/download.rs | 184 |
1 files changed, 95 insertions, 89 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index 7af1066..7ea5c29 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | #![allow(clippy::too_many_lines)] | ||
2 | |||
3 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 1 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
4 | 2 | ||
5 | use crate::{config::Cfg, List}; | 3 | use crate::{config::Cfg, List}; |
@@ -35,105 +33,113 @@ pub async fn download( | |||
35 | ); | 33 | ); |
36 | 34 | ||
37 | for current_list in liststack { | 35 | for current_list in liststack { |
38 | download_p.set_message(format!("Download in {}", current_list.id)); | 36 | download_list(config, mp.clone(), download_p.clone(), current_list, clean, delete_old).await?; |
39 | 37 | } | |
40 | let downloaded_versions = get_downloaded_versions(¤t_list)?; | ||
41 | let current_version_ids = | ||
42 | match userlist_get_all_current_versions_with_mods( | ||
43 | config, | ||
44 | ¤t_list.id, | ||
45 | ) { | ||
46 | Ok(i) => Ok(i), | ||
47 | Err(e) => { | ||
48 | Err(MLErr::new(EType::DBError, e.to_string().as_str())) | ||
49 | } | ||
50 | }?; | ||
51 | |||
52 | let mut to_download: Vec<String> = vec![]; | ||
53 | //(mod_id, version_id) | ||
54 | let mut to_disable: Vec<(String, String)> = vec![]; | ||
55 | |||
56 | for version in current_version_ids { | ||
57 | let mod_id = version.0; | ||
58 | let current_version = version.1; | ||
59 | |||
60 | let current_download = downloaded_versions.get(&mod_id); | ||
61 | |||
62 | if current_download.is_none() || clean { | ||
63 | to_download.push(current_version); | ||
64 | } else { | ||
65 | let downloaded_version = current_download | ||
66 | .ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; | ||
67 | if ¤t_version != downloaded_version { | ||
68 | to_disable.push(( | ||
69 | mod_id.clone(), | ||
70 | String::from(downloaded_version), | ||
71 | )); | ||
72 | to_download.push(current_version); | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | 38 | ||
77 | if clean { | 39 | download_p.finish_with_message("Downloaded all lists"); |
78 | clean_list_dir(¤t_list)?; | 40 | |
79 | }; | 41 | Ok(()) |
42 | } | ||
43 | |||
44 | async fn download_list( | ||
45 | config: &Cfg, | ||
46 | mp: MultiProgress, | ||
47 | download_p: ProgressBar, | ||
48 | current_list: List, | ||
49 | clean: bool, | ||
50 | delete_old: bool, | ||
51 | ) -> MLE<()> { | ||
52 | download_p.set_message(format!("Download in {}", current_list.id)); | ||
53 | |||
54 | let downloaded_versions = get_downloaded_versions(¤t_list)?; | ||
55 | let current_version_ids = match userlist_get_all_current_versions_with_mods( | ||
56 | config, | ||
57 | ¤t_list.id, | ||
58 | ) { | ||
59 | Ok(i) => Ok(i), | ||
60 | Err(e) => Err(MLErr::new(EType::DBError, e.to_string().as_str())), | ||
61 | }?; | ||
62 | |||
63 | let mut to_download: Vec<String> = vec![]; | ||
64 | //(mod_id, version_id) | ||
65 | let mut to_disable: Vec<(String, String)> = vec![]; | ||
66 | |||
67 | for version in current_version_ids { | ||
68 | let mod_id = version.0; | ||
69 | let current_version = version.1; | ||
80 | 70 | ||
81 | if to_download.is_empty() { | 71 | let current_download = downloaded_versions.get(&mod_id); |
82 | download_p.println(format!( | 72 | |
83 | "There are no new versions to download for {}", | 73 | if current_download.is_none() || clean { |
84 | current_list.id | 74 | to_download.push(current_version); |
85 | )); | ||
86 | } else { | 75 | } else { |
87 | download_versions( | 76 | let downloaded_version = |
88 | current_list.clone(), | 77 | current_download.ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; |
89 | config.clone(), | 78 | if ¤t_version != downloaded_version { |
90 | get_raw_versions(&config.apis.modrinth, to_download).await?, | 79 | to_disable |
91 | &mp, | 80 | .push((mod_id.clone(), String::from(downloaded_version))); |
92 | &download_p, | 81 | to_download.push(current_version); |
93 | ) | 82 | } |
94 | .await?; | ||
95 | } | 83 | } |
84 | } | ||
96 | 85 | ||
97 | if !to_disable.is_empty() { | 86 | if clean { |
98 | let d_p = | 87 | clean_list_dir(¤t_list)?; |
99 | mp.insert_before( | 88 | }; |
100 | &download_p, | 89 | |
101 | ProgressBar::new(to_disable.len().try_into().map_err( | 90 | if to_download.is_empty() { |
102 | |_| MLErr::new(EType::Other, "ListStackLen"), | 91 | download_p.println(format!( |
103 | )?), | 92 | "There are no new versions to download for {}", |
104 | ); | 93 | current_list.id |
105 | d_p.set_style( | 94 | )); |
106 | ProgressStyle::with_template(STYLE_BAR_POS) | 95 | } else { |
107 | .map_err(|_| { | 96 | download_versions( |
108 | MLErr::new(EType::LibIndicatif, "template error") | 97 | current_list.clone(), |
109 | })? | 98 | config.clone(), |
110 | .progress_chars(PROGRESS_CHARS), | 99 | get_raw_versions(&config.apis.modrinth, to_download).await?, |
111 | ); | 100 | &mp, |
112 | for ver in to_disable { | 101 | &download_p, |
113 | if delete_old { | 102 | ) |
114 | d_p.set_message(format!("Delete version {}", ver.1)); | 103 | .await?; |
115 | d_p.inc(1); | 104 | } |
116 | delete_version(¤t_list, &ver.1)?; | ||
117 | } else { | ||
118 | d_p.set_message(format!("Disable version {}", ver.1)); | ||
119 | d_p.inc(1); | ||
120 | disable_version(config, ¤t_list, ver.1, ver.0)?; | ||
121 | }; | ||
122 | } | ||
123 | 105 | ||
124 | let del_msg = if delete_old { | 106 | if !to_disable.is_empty() { |
125 | "Deleted all old versions" | 107 | let d_p = mp.insert_before( |
108 | &download_p, | ||
109 | ProgressBar::new( | ||
110 | to_disable | ||
111 | .len() | ||
112 | .try_into() | ||
113 | .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, | ||
114 | ), | ||
115 | ); | ||
116 | d_p.set_style( | ||
117 | ProgressStyle::with_template(STYLE_BAR_POS) | ||
118 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? | ||
119 | .progress_chars(PROGRESS_CHARS), | ||
120 | ); | ||
121 | for ver in to_disable { | ||
122 | if delete_old { | ||
123 | d_p.set_message(format!("Delete version {}", ver.1)); | ||
124 | d_p.inc(1); | ||
125 | delete_version(¤t_list, &ver.1)?; | ||
126 | } else { | 126 | } else { |
127 | "Disabled all old versions" | 127 | d_p.set_message(format!("Disable version {}", ver.1)); |
128 | d_p.inc(1); | ||
129 | disable_version(config, ¤t_list, ver.1, ver.0)?; | ||
128 | }; | 130 | }; |
129 | |||
130 | d_p.finish_with_message(del_msg); | ||
131 | } | 131 | } |
132 | 132 | ||
133 | download_p.inc(1); | 133 | let del_msg = if delete_old { |
134 | "Deleted all old versions" | ||
135 | } else { | ||
136 | "Disabled all old versions" | ||
137 | }; | ||
138 | |||
139 | d_p.finish_with_message(del_msg); | ||
134 | } | 140 | } |
135 | 141 | ||
136 | download_p.finish_with_message("Downloaded all lists"); | 142 | download_p.inc(1); |
137 | 143 | ||
138 | Ok(()) | 144 | Ok(()) |
139 | } | 145 | } |