diff options
author | fxqnlr <[email protected]> | 2022-11-17 21:20:09 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-11-17 21:20:09 +0100 |
commit | fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113 (patch) | |
tree | ec7c7c80434b339f9442882f1e2dce6f60cc9edd /src/commands/update.rs | |
parent | 5145dd23f1777180d8003e76f59af57643796516 (diff) | |
download | modlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.tar modlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.tar.gz modlist-fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113.zip |
added --clean for update && list downloadfolder
Diffstat (limited to 'src/commands/update.rs')
-rw-r--r-- | src/commands/update.rs | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs index e383eae..85630f5 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -4,9 +4,9 @@ use reqwest::Client; | |||
4 | 4 | ||
5 | use futures_util::StreamExt; | 5 | use futures_util::StreamExt; |
6 | 6 | ||
7 | use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List}; | 7 | use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input}; |
8 | 8 | ||
9 | pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | 9 | pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
10 | 10 | ||
11 | let current_list = get_current_list(config.clone())?; | 11 | let current_list = get_current_list(config.clone())?; |
12 | 12 | ||
@@ -20,15 +20,16 @@ pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | |||
20 | 20 | ||
21 | let mut updatestack: Vec<Version> = vec![]; | 21 | let mut updatestack: Vec<Version> = vec![]; |
22 | for (index, project) in projects.into_iter().enumerate() { | 22 | for (index, project) in projects.into_iter().enumerate() { |
23 | //Get versions for project and check if they match up | ||
23 | let current_version = &versions[index]; | 24 | let current_version = &versions[index]; |
24 | |||
25 | let p_id = String::from(&project.id); | 25 | let p_id = String::from(&project.id); |
26 | let v_id = ¤t_version.mod_id; | 26 | let v_id = ¤t_version.mod_id; |
27 | |||
28 | if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; | 27 | if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; |
29 | 28 | ||
30 | if project.versions.join("|") != current_version.versions { | 29 | |
31 | updatestack.push(match specific_update(config.clone(), current_list.clone(), project).await { | 30 | //Adding to stack if not the same versions in the list OR if clean == true |
31 | if input.clone().clean || (project.versions.join("|") != current_version.versions) { | ||
32 | updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project).await { | ||
32 | Ok(ver) => ver, | 33 | Ok(ver) => ver, |
33 | //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") | 34 | //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") |
34 | Err(_) => { continue; }, | 35 | Err(_) => { continue; }, |
@@ -36,13 +37,23 @@ pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | |||
36 | }; | 37 | }; |
37 | }; | 38 | }; |
38 | //println!("{:?}", updatestack); | 39 | //println!("{:?}", updatestack); |
40 | |||
41 | |||
42 | if input.clean { | ||
43 | let dl_path = ¤t_list.download_folder; | ||
44 | println!("Cleaning {}", dl_path); | ||
45 | for entry in std::fs::read_dir(dl_path)? { | ||
46 | let entry = entry?; | ||
47 | std::fs::remove_file(entry.path())?; | ||
48 | } | ||
49 | } | ||
39 | 50 | ||
40 | download_updates(config, updatestack).await?; | 51 | download_updates(config, current_list, updatestack).await?; |
41 | 52 | ||
42 | Ok(()) | 53 | Ok(()) |
43 | } | 54 | } |
44 | 55 | ||
45 | async fn specific_update(config: Cfg, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> { | 56 | async fn specific_update(config: Cfg, input: Input, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> { |
46 | print!("Checking update for '{}' in {}", project.title, list.id); | 57 | print!("Checking update for '{}' in {}", project.title, list.id); |
47 | 58 | ||
48 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | 59 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
@@ -54,7 +65,7 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result<Ve | |||
54 | } | 65 | } |
55 | 66 | ||
56 | let mut current: Vec<Version> = vec![]; | 67 | let mut current: Vec<Version> = vec![]; |
57 | if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? { | 68 | if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { |
58 | //get new versions | 69 | //get new versions |
59 | print!(" | getting new version"); | 70 | print!(" | getting new version"); |
60 | let current_str = extract_current_version(applicable_versions.clone())?; | 71 | let current_str = extract_current_version(applicable_versions.clone())?; |
@@ -71,9 +82,9 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result<Ve | |||
71 | Ok(current[0].clone()) | 82 | Ok(current[0].clone()) |
72 | } | 83 | } |
73 | 84 | ||
74 | async fn download_updates(config: Cfg, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | 85 | async fn download_updates(config: Cfg, current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
75 | 86 | ||
76 | let dl_path = String::from(&config.downloads); | 87 | let dl_path = String::from(¤t_list.download_folder); |
77 | 88 | ||
78 | for ver in versions { | 89 | for ver in versions { |
79 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); | 90 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); |