summaryrefslogtreecommitdiff
path: root/src/commands/update.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-23 14:13:03 +0200
committerfx <[email protected]>2023-04-23 14:13:03 +0200
commit4300ad2eb05dddfa4274e04b204f2ad28c87da05 (patch)
treea2fd059e3aefff812d0d25e23fc85203dc9d122a /src/commands/update.rs
parent2711f05669e353fbf452156d54855e9ba454f4a8 (diff)
parent64958cc9ff0858dbf068625e35b8d5dae249d4a4 (diff)
downloadmodlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar.gz
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.zip
Merge pull request 'clap' (#1) from clap into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/1
Diffstat (limited to 'src/commands/update.rs')
-rw-r--r--src/commands/update.rs132
1 files changed, 87 insertions, 45 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 75bee39..3d9578b 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,29 +1,30 @@
1use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, userlist_get_set_version, mods_get_info}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; 1use crate::{
2 2 config::Cfg,
3pub async fn update(config: Cfg, input: Input) -> MLE<()> { 3 db::{
4 let mut liststack: Vec<List> = vec![]; 4 mods_get_info, userlist_change_versions, userlist_get_all_ids,
5 if input.all_lists { 5 userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version,
6 let list_ids = lists_get_all_ids(config.clone())?; 6 },
7 for id in list_ids { 7 error::{ErrorType, MLError, MLE},
8 liststack.push(lists_get(config.clone(), id)?); 8 files::{clean_list_dir, delete_version, disable_version, download_versions},
9 } 9 modrinth::{extract_current_version, versions, Version},
10 } else { 10 List,
11 let current = get_current_list(config.clone())?; 11};
12 println!("Update list {}:", current.id); 12
13 liststack.push(current) 13pub async fn update(
14 } 14 config: Cfg,
15 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await 15 liststack: Vec<List>,
16} 16 clean: bool,
17 17 direct_download: bool,
18pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { 18 delete_old: bool,
19) -> MLE<()> {
19 for current_list in liststack { 20 for current_list in liststack {
20 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 21 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
21 22
22 let mut current_versions: Vec<(String, String)> = vec![]; 23 let mut current_versions: Vec<(String, String)> = vec![];
23 24
24 println!(" └Update mods:"); 25 println!(" └Update mods:");
25 let mut updatestack: Vec<Version> = vec![]; 26 let mut updatestack: Vec<Version> = vec![];
26 27
27 for id in mods { 28 for id in mods {
28 let info = mods_get_info(config.clone(), &id)?; 29 let info = mods_get_info(config.clone(), &id)?;
29 println!("\t└{}", info.title); 30 println!("\t└{}", info.title);
@@ -34,27 +35,39 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
34 } 35 }
35 36
36 //Getting current installed version for disable or delete 37 //Getting current installed version for disable or delete
37 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&id))?; 38 let disable_version =
39 userlist_get_current_version(config.clone(), &current_list.id, &id)?;
38 40
39 updatestack.push( 41 updatestack.push(
40 match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await { 42 match specific_update(
43 config.clone(),
44 clean,
45 current_list.clone(),
46 String::from(&id),
47 )
48 .await
49 {
41 Ok(ver) => { 50 Ok(ver) => {
42 current_versions.push((disable_version, id)); 51 current_versions.push((disable_version, id));
43 ver 52 ver
44 }, 53 }
45 Err(e) => { 54 Err(e) => {
46 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { 55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
47 println!("\t └No new version found for the specified minecraft version"); 56 println!(
57 "\t └No new version found for the specified minecraft version"
58 );
48 } else { 59 } else {
49 return Err(e); 60 return Err(e);
50 }; 61 };
51 continue; 62 continue;
52 } 63 }
53 } 64 },
54 ) 65 )
55 }; 66 }
56 67
57 if clean { clean_list_dir(&current_list)?; }; 68 if clean {
69 clean_list_dir(&current_list)?;
70 };
58 71
59 if direct_download && !updatestack.is_empty() { 72 if direct_download && !updatestack.is_empty() {
60 download_versions(current_list.clone(), config.clone(), updatestack).await?; 73 download_versions(current_list.clone(), config.clone(), updatestack).await?;
@@ -65,7 +78,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
65 if delete_old { 78 if delete_old {
66 println!("\t └Delete version {}", ver.0); 79 println!("\t └Delete version {}", ver.0);
67 delete_version(current_list.clone(), ver.0)?; 80 delete_version(current_list.clone(), ver.0)?;
68 } else if ver.0 != "NONE" { 81 } else if ver.0 != "NONE" {
69 println!("\t └Disable version {}", ver.0); 82 println!("\t └Disable version {}", ver.0);
70 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; 83 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
71 }; 84 };
@@ -78,10 +91,11 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
78} 91}
79 92
80async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> { 93async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> {
81 let applicable_versions = versions(&config.apis.modrinth, String::from(&id), list.clone()).await; 94 let applicable_versions =
82 95 versions(&config.apis.modrinth, String::from(&id), list.clone()).await;
96
83 let mut versions: Vec<String> = vec![]; 97 let mut versions: Vec<String> = vec![];
84 98
85 if !applicable_versions.is_empty() { 99 if !applicable_versions.is_empty() {
86 for ver in &applicable_versions { 100 for ver in &applicable_versions {
87 versions.push(String::from(&ver.id)); 101 versions.push(String::from(&ver.id));
@@ -92,8 +106,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
92 106
93 let mut current: Vec<Version> = vec![]; 107 let mut current: Vec<Version> = vec![];
94 //TODO Split clean and no match 108 //TODO Split clean and no match
95 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&id))?) { 109 if clean
96 110 || (versions.join("|")
111 != userlist_get_applicable_versions(
112 config.clone(),
113 String::from(&list.id),
114 String::from(&id),
115 )?)
116 {
97 let current_str = extract_current_version(applicable_versions.clone())?; 117 let current_str = extract_current_version(applicable_versions.clone())?;
98 118
99 if clean { 119 if clean {
@@ -104,34 +124,54 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
104 }; 124 };
105 125
106 //get new versions 126 //get new versions
107 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { 127 let current_ver = match applicable_versions
128 .into_iter()
129 .find(|ver| ver.id == current_str)
130 .ok_or("!no current version in applicable_versions")
131 {
108 Ok(v) => Ok(v), 132 Ok(v) => Ok(v),
109 Err(e) => Err(MLError::new(ErrorType::Other, e)), 133 Err(e) => Err(MLError::new(ErrorType::Other, e)),
110 }?; 134 }?;
111 current.push(current_ver.clone()); 135 current.push(current_ver.clone());
112 136
113 let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { 137 //TODO implement version selection if no primary
138 let link = match current_ver
139 .files
140 .into_iter()
141 .find(|f| f.primary)
142 .ok_or("!no primary in links")
143 {
114 Ok(p) => Ok(p), 144 Ok(p) => Ok(p),
115 Err(e) => Err(MLError::new(ErrorType::Other, e)), 145 Err(e) => Err(MLError::new(ErrorType::Other, e)),
116 }?.url; 146 }?
147 .url;
117 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; 148 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?;
118 } 149 }
119 150
120 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; 151 if current.is_empty() {
121 152 return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE"));
153 };
154
122 //println!(" └✔️"); 155 //println!(" └✔️");
123 Ok(current[0].clone()) 156 Ok(current[0].clone())
124} 157}
125 158
126#[tokio::test] 159#[tokio::test]
127async fn download_updates_test() { 160async fn download_updates_test() {
161 use crate::{
162 modrinth::{Hash, Version, VersionFile, VersionType},
163 List, Modloader,
164 };
128 165
129 use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List};
130
131 let config = Cfg::init("modlist.toml").unwrap(); 166 let config = Cfg::init("modlist.toml").unwrap();
132 let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Fabric, download_folder: String::from("./dev/tests/dl") }; 167 let current_list = List {
133 168 id: String::from("..."),
134 let versions = vec![Version { 169 mc_version: String::from("..."),
170 modloader: Modloader::Fabric,
171 download_folder: String::from("./dev/tests/dl"),
172 };
173
174 let versions = vec![Version {
135 id: "dEqtGnT9".to_string(), 175 id: "dEqtGnT9".to_string(),
136 project_id: "kYuIpRLv".to_string(), 176 project_id: "kYuIpRLv".to_string(),
137 author_id: "Qnt13hO8".to_string(), 177 author_id: "Qnt13hO8".to_string(),
@@ -161,5 +201,7 @@ async fn download_updates_test() {
161 "fabric".to_string() 201 "fabric".to_string()
162 ] 202 ]
163 }]; 203 }];
164 assert!(download_versions(current_list, config, versions).await.is_ok()) 204 assert!(download_versions(current_list, config, versions)
205 .await
206 .is_ok())
165} 207}