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.rs95
1 files changed, 60 insertions, 35 deletions
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 194bbe5..3aae002 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,4 +1,4 @@
1use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; 1use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
2 2
3use crate::{ 3use crate::{
4 config::Cfg, 4 config::Cfg,
@@ -19,11 +19,14 @@ pub async fn update(
19 direct_download: bool, 19 direct_download: bool,
20 delete_old: bool, 20 delete_old: bool,
21) -> MLE<()> { 21) -> MLE<()> {
22
23 let mp = MultiProgress::new(); 22 let mp = MultiProgress::new();
24 23
25 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); 24 let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap()));
26 update_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 25 update_p.set_style(
26 ProgressStyle::with_template(STYLE_BAR_POS)
27 .unwrap()
28 .progress_chars(PROGRESS_CHARS),
29 );
27 30
28 for current_list in liststack { 31 for current_list in liststack {
29 update_p.set_message(format!("Update {}", current_list.id)); 32 update_p.set_message(format!("Update {}", current_list.id));
@@ -35,7 +38,11 @@ pub async fn update(
35 let mods = userlist_get_all_ids(config, &current_list.id)?; 38 let mods = userlist_get_all_ids(config, &current_list.id)?;
36 39
37 let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap())); 40 let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap()));
38 list_u_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 41 list_u_p.set_style(
42 ProgressStyle::with_template(STYLE_BAR_POS)
43 .unwrap()
44 .progress_chars(PROGRESS_CHARS),
45 );
39 46
40 let mut current_versions: Vec<(String, String)> = vec![]; 47 let mut current_versions: Vec<(String, String)> = vec![];
41 let mut updatestack: Vec<Version> = vec![]; 48 let mut updatestack: Vec<Version> = vec![];
@@ -43,7 +50,7 @@ pub async fn update(
43 for id in mods { 50 for id in mods {
44 let info = mods_get_info(config, &id)?; 51 let info = mods_get_info(config, &id)?;
45 list_u_p.set_message(format!("Update {}", info.title)); 52 list_u_p.set_message(format!("Update {}", info.title));
46 53
47 //Skip check if version is set 54 //Skip check if version is set
48 if userlist_get_set_version(config, &current_list.id, &id)? { 55 if userlist_get_set_version(config, &current_list.id, &id)? {
49 list_u_p.inc(1); 56 list_u_p.inc(1);
@@ -51,19 +58,10 @@ pub async fn update(
51 } 58 }
52 59
53 //Getting current installed version for disable or delete 60 //Getting current installed version for disable or delete
54 let disable_version = 61 let disable_version = userlist_get_current_version(config, &current_list.id, &id)?;
55 userlist_get_current_version(config, &current_list.id, &id)?;
56 62
57 updatestack.push( 63 updatestack.push(
58 match specific_update( 64 match specific_update(config, clean, current_list.clone(), &id, &list_u_p).await {
59 config,
60 clean,
61 current_list.clone(),
62 &id,
63 &list_u_p
64 )
65 .await
66 {
67 Ok(ver) => { 65 Ok(ver) => {
68 current_versions.push((disable_version, id)); 66 current_versions.push((disable_version, id));
69 ver 67 ver
@@ -89,17 +87,31 @@ pub async fn update(
89 }; 87 };
90 88
91 if direct_download && !updatestack.is_empty() { 89 if direct_download && !updatestack.is_empty() {
92 download_versions(current_list.clone(), config.clone(), updatestack, &mp, &list_p).await?; 90 download_versions(
91 current_list.clone(),
92 config.clone(),
93 updatestack,
94 &mp,
95 &list_p,
96 )
97 .await?;
93 98
94 //Disable old versions 99 //Disable old versions
95 if !clean { 100 if !clean {
96 let d_p = mp.insert_before(&list_p, ProgressBar::new(current_versions.len().try_into().unwrap())); 101 let d_p = mp.insert_before(
97 d_p.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); 102 &list_p,
103 ProgressBar::new(current_versions.len().try_into().unwrap()),
104 );
105 d_p.set_style(
106 ProgressStyle::with_template(STYLE_BAR_POS)
107 .unwrap()
108 .progress_chars(PROGRESS_CHARS),
109 );
98 for ver in current_versions { 110 for ver in current_versions {
99 if delete_old { 111 if delete_old {
100 d_p.set_message(format!("Delete version {}", ver.0)); 112 d_p.set_message(format!("Delete version {}", ver.0));
101 d_p.inc(1); 113 d_p.inc(1);
102 delete_version(current_list.clone(), ver.0)?; 114 delete_version(&current_list, ver.0)?;
103 } else if ver.0 != "NONE" { 115 } else if ver.0 != "NONE" {
104 d_p.set_message(format!("Disable version {}", ver.0)); 116 d_p.set_message(format!("Disable version {}", ver.0));
105 d_p.inc(1); 117 d_p.inc(1);
@@ -125,9 +137,14 @@ pub async fn update(
125 Ok(()) 137 Ok(())
126} 138}
127 139
128async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progress: &ProgressBar) -> MLE<Version> { 140async fn specific_update(
129 let applicable_versions = 141 config: &Cfg,
130 versions(&config.apis.modrinth, String::from(id), list.clone()).await; 142 clean: bool,
143 list: List,
144 id: &str,
145 progress: &ProgressBar,
146) -> MLE<Version> {
147 let applicable_versions = versions(&config.apis.modrinth, String::from(id), list.clone()).await;
131 148
132 let mut versions: Vec<String> = vec![]; 149 let mut versions: Vec<String> = vec![];
133 150
@@ -142,15 +159,16 @@ async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progre
142 let mut current: Vec<Version> = vec![]; 159 let mut current: Vec<Version> = vec![];
143 if clean 160 if clean
144 || (versions.join("|") 161 || (versions.join("|")
145 != userlist_get_applicable_versions( 162 != userlist_get_applicable_versions(config, String::from(&list.id), String::from(id))?)
146 config,
147 String::from(&list.id),
148 String::from(id),
149 )?)
150 { 163 {
151 let current_str = extract_current_version(applicable_versions.clone())?; 164 let current_str = extract_current_version(applicable_versions.clone())?;
152 165
153 if !clean { progress.println(format!("Found new version for {}", mods_get_info(config, id).unwrap().title)); } 166 if !clean {
167 progress.println(format!(
168 "Found new version for {}",
169 mods_get_info(config, id).unwrap().title
170 ));
171 }
154 172
155 //get new versions 173 //get new versions
156 let current_ver = match applicable_versions 174 let current_ver = match applicable_versions
@@ -166,12 +184,19 @@ async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progre
166 let files = &current_ver.files; 184 let files = &current_ver.files;
167 185
168 let link = match files.clone().into_iter().find(|f| f.primary) { 186 let link = match files.clone().into_iter().find(|f| f.primary) {
169 Some(f) => f, 187 Some(f) => f,
170 None => { files[0].clone() } 188 None => files[0].clone(),
171 } 189 }
172 .url; 190 .url;
173 191
174 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id.to_string())?; 192 userlist_change_versions(
193 config,
194 list.id,
195 current_str,
196 versions.join("|"),
197 link,
198 id.to_string(),
199 )?;
175 } 200 }
176 201
177 if current.is_empty() { 202 if current.is_empty() {