summaryrefslogtreecommitdiff
path: root/src/commands/update.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/update.rs')
-rw-r--r--src/commands/update.rs79
1 files changed, 57 insertions, 22 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index d3a282b..2de13f3 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,3 +1,5 @@
1use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
2
1use crate::{ 3use crate::{
2 config::Cfg, 4 config::Cfg,
3 db::{ 5 db::{
@@ -10,6 +12,8 @@ use crate::{
10 List, 12 List,
11}; 13};
12 14
15const PROGRESS_CHARS: &str = "#>-";
16
13pub async fn update( 17pub async fn update(
14 config: Cfg, 18 config: Cfg,
15 liststack: Vec<List>, 19 liststack: Vec<List>,
@@ -17,20 +21,39 @@ pub async fn update(
17 direct_download: bool, 21 direct_download: bool,
18 delete_old: bool, 22 delete_old: bool,
19) -> MLE<()> { 23) -> MLE<()> {
24
25 let mp = MultiProgress::new();
26
27 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap()));
28 let bar_style = ProgressStyle::with_template("{spinner:.green}{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS);
29 let spinner_style = ProgressStyle::with_template("{spinner:.green}{msg}").unwrap();
30 update_p.set_style(bar_style.clone());
31 update_p.set_message("Update");
32
20 for current_list in liststack { 33 for current_list in liststack {
21 println!("Update mods in {}", current_list.id); 34
35 // println!("Update mods in {}", current_list.id);
22 let mods = userlist_get_all_ids(config.clone(), &current_list.id)?; 36 let mods = userlist_get_all_ids(config.clone(), &current_list.id)?;
23 37
38 let list_p = mp.insert_before(&update_p, ProgressBar::new(mods.len().try_into().unwrap()));
39 list_p.set_style(bar_style.clone());
40 list_p.set_message(format!("Update {}", current_list.id));
41
24 let mut current_versions: Vec<(String, String)> = vec![]; 42 let mut current_versions: Vec<(String, String)> = vec![];
25 43
26 let mut updatestack: Vec<Version> = vec![]; 44 let mut updatestack: Vec<Version> = vec![];
27 45
28 for id in mods { 46 for id in mods {
29 let info = mods_get_info(config.clone(), &id)?; 47 let mod_p = mp.insert_before(&list_p, ProgressBar::new(1));
30 println!(" ├{}", info.title); 48 mod_p.set_style(spinner_style.clone());
49
50 let info = mods_get_info(&config, &id)?;
51 mod_p.set_message(format!("Update {}", info.title));
52 // println!(" ├{}", info.title);
31 53
32 if userlist_get_set_version(config.clone(), &current_list.id, &id)? { 54 if userlist_get_set_version(config.clone(), &current_list.id, &id)? {
33 println!(" │ └Set version, skipping update"); 55 // println!(" │ └Set version, skipping update");
56 list_p.inc(1);
34 continue; 57 continue;
35 } 58 }
36 59
@@ -38,12 +61,15 @@ pub async fn update(
38 let disable_version = 61 let disable_version =
39 userlist_get_current_version(config.clone(), &current_list.id, &id)?; 62 userlist_get_current_version(config.clone(), &current_list.id, &id)?;
40 63
64 mod_p.inc(1);
65
41 updatestack.push( 66 updatestack.push(
42 match specific_update( 67 match specific_update(
43 config.clone(), 68 config.clone(),
44 clean, 69 clean,
45 current_list.clone(), 70 current_list.clone(),
46 String::from(&id), 71 String::from(&id),
72 &mod_p
47 ) 73 )
48 .await 74 .await
49 { 75 {
@@ -53,19 +79,25 @@ pub async fn update(
53 } 79 }
54 Err(e) => { 80 Err(e) => {
55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { 81 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
56 println!( 82 // println!(
57 " │ └No new version found for the specified minecraft version" 83 // " │ └No new version found for the specified minecraft version"
58 ); 84 // );
59 } else { 85 } else {
60 return Err(e); 86 return Err(e);
61 }; 87 };
88 list_p.inc(1);
62 continue; 89 continue;
63 } 90 }
64 }, 91 },
65 ) 92 );
93 list_p.inc(1);
66 } 94 }
67 95
96 list_p.finish_with_message(format!("Updated {}", current_list.id));
97
68 if clean { 98 if clean {
99 update_p.set_message("Cleaning");
100 update_p.inc(1);
69 clean_list_dir(&current_list)?; 101 clean_list_dir(&current_list)?;
70 }; 102 };
71 103
@@ -85,12 +117,16 @@ pub async fn update(
85 } 117 }
86 } 118 }
87 }; 119 };
120 update_p.inc(1);
88 } 121 }
89 122
123 update_p.finish_with_message("Updated all lists");
124
125
90 Ok(()) 126 Ok(())
91} 127}
92 128
93async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE<Version> { 129async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progress: &ProgressBar) -> MLE<Version> {
94 let applicable_versions = 130 let applicable_versions =
95 versions(&config.apis.modrinth, String::from(&id), list.clone()).await; 131 versions(&config.apis.modrinth, String::from(&id), list.clone()).await;
96 132
@@ -116,10 +152,11 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
116 let current_str = extract_current_version(applicable_versions.clone())?; 152 let current_str = extract_current_version(applicable_versions.clone())?;
117 153
118 if clean { 154 if clean {
119 println!("\t └Add version to downloadstack"); 155 // println!("\t └Add version to downloadstack");
120 } else { 156 } else {
121 println!("\t └Get versions for specified minecraft versions"); 157 progress.println(format!("Found new version for {}", mods_get_info(&config, &id).unwrap().title));
122 println!("\t └New current version: {}", current_str); 158 // println!("\t └Get versions for specified minecraft versions");
159 // println!("\t └New current version: {}", current_str);
123 }; 160 };
124 161
125 //get new versions 162 //get new versions
@@ -133,16 +170,14 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML
133 }?; 170 }?;
134 current.push(current_ver.clone()); 171 current.push(current_ver.clone());
135 172
136 let link = match current_ver 173 let files = &current_ver.files;
137 .files 174
138 .into_iter() 175 let link = match files.clone().into_iter().find(|f| f.primary) {
139 .find(|f| f.primary) 176 Some(f) => f,
140 .ok_or("!no primary in links") 177 None => { files[0].clone() }
141 { 178 }
142 Ok(p) => Ok(p), 179 .url;
143 Err(e) => Err(MLError::new(ErrorType::Other, e)), 180
144 }?
145 .url;
146 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; 181 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?;
147 } 182 }
148 183