diff options
author | fxqnlr <[email protected]> | 2022-12-18 23:11:50 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-12-18 23:11:50 +0100 |
commit | 28706f6edf10a135a67334d7035948bab4064bef (patch) | |
tree | 64ce9c6aa58167e0b9ffbca968c1aaab224cea73 /src/commands/download.rs | |
parent | dc5955955785cdabea90c010db65b661ab87e2dc (diff) | |
download | modlist-28706f6edf10a135a67334d7035948bab4064bef.tar modlist-28706f6edf10a135a67334d7035948bab4064bef.tar.gz modlist-28706f6edf10a135a67334d7035948bab4064bef.zip |
dl add clean & all-lists; start of io
Diffstat (limited to 'src/commands/download.rs')
-rw-r--r-- | src/commands/download.rs | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index 9f70499..0f9011c 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -1,49 +1,69 @@ | |||
1 | use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::userlist_get_all_current_versions_with_mods, modrinth::get_raw_versions}; | 1 | use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions}; |
2 | #[allow(unused_imports)] | 2 | use crate::{List, get_current_list, config::Cfg, input::Input}; |
3 | use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; | ||
4 | 3 | ||
5 | pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 4 | pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | 5 | ||
7 | let current_list = get_current_list(config.clone())?; | 6 | let mut liststack: Vec<List> = vec![]; |
7 | if input.all_lists { | ||
8 | let list_ids = lists_get_all_ids(config.clone())?; | ||
9 | for id in list_ids { | ||
10 | liststack.push(lists_get(config.clone(), id)?); | ||
11 | } | ||
12 | } else { | ||
13 | let current = get_current_list(config.clone())?; | ||
14 | println!("Checking for updates of mods in {}", current.id); | ||
15 | liststack.push(current) | ||
16 | } | ||
8 | 17 | ||
9 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; | 18 | for current_list in liststack { |
10 | let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; | 19 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; |
20 | let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; | ||
11 | 21 | ||
12 | let mut to_download: Vec<String> = vec![]; | 22 | let mut to_download: Vec<String> = vec![]; |
13 | //(mod_id, version_id) | 23 | //(mod_id, version_id) |
14 | let mut to_disable: Vec<(String, String)> = vec![]; | 24 | let mut to_disable: Vec<(String, String)> = vec![]; |
15 | 25 | ||
16 | for version in current_version_ids { | 26 | for version in current_version_ids { |
17 | let mod_id = version.0; | 27 | let mod_id = version.0; |
18 | let current_version = version.1; | 28 | let current_version = version.1; |
19 | 29 | ||
20 | let current_download = downloaded_versions.get(&mod_id); | 30 | let current_download = downloaded_versions.get(&mod_id); |
21 | 31 | ||
22 | if current_download.is_none() { | 32 | if current_download.is_none() || input.clean { |
23 | to_download.push(current_version); | ||
24 | } else { | ||
25 | let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; | ||
26 | if ¤t_version != downloaded_version { | ||
27 | to_disable.push((mod_id.clone(), String::from(downloaded_version))); | ||
28 | to_download.push(current_version); | 33 | to_download.push(current_version); |
34 | } else { | ||
35 | let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; | ||
36 | if ¤t_version != downloaded_version { | ||
37 | to_disable.push((mod_id.clone(), String::from(downloaded_version))); | ||
38 | to_download.push(current_version); | ||
39 | } | ||
29 | } | 40 | } |
30 | } | 41 | } |
31 | } | 42 | |
32 | 43 | if input.clean { | |
33 | if !to_download.is_empty() { | 44 | let dl_path = ¤t_list.download_folder; |
34 | download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; | 45 | println!("Cleaning {}", dl_path); |
35 | } else { | 46 | for entry in std::fs::read_dir(dl_path)? { |
36 | println!("There are no new versions to download"); | 47 | let entry = entry?; |
37 | } | 48 | std::fs::remove_file(entry.path())?; |
38 | 49 | } | |
39 | if !to_disable.is_empty() { | 50 | } |
40 | for ver in to_disable { | 51 | |
41 | if input.delete_old { | 52 | if !to_download.is_empty() { |
42 | println!("Deleting version {} for mod {}", ver.1, ver.0); | 53 | download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; |
43 | delete_version(current_list.clone(), ver.1)?; | 54 | } else { |
44 | } else { | 55 | println!("There are no new versions to download"); |
45 | disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; | 56 | } |
46 | }; | 57 | |
58 | if !to_disable.is_empty() { | ||
59 | for ver in to_disable { | ||
60 | if input.delete_old { | ||
61 | println!("Deleting version {} for mod {}", ver.1, ver.0); | ||
62 | delete_version(current_list.clone(), ver.1)?; | ||
63 | } else { | ||
64 | disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; | ||
65 | }; | ||
66 | } | ||
47 | } | 67 | } |
48 | } | 68 | } |
49 | 69 | ||