From c6061b519822511a92975d56562dd3c7181ed421 Mon Sep 17 00:00:00 2001
From: fxqnlr <felixquinn03@gmail.com>
Date: Sat, 7 Jan 2023 13:25:00 +0100
Subject: moved update in own function

---
 Cargo.lock             |  2 +-
 Cargo.toml             |  2 +-
 src/commands/update.rs | 24 ++++++++++++++----------
 3 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 788f68c..ac1cd5e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -615,7 +615,7 @@ dependencies = [
 
 [[package]]
 name = "modlist"
-version = "0.8.0"
+version = "0.8.1"
 dependencies = [
  "chrono",
  "dirs",
diff --git a/Cargo.toml b/Cargo.toml
index 6e9c282..76b31d4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "modlist"
-version = "0.8.0"
+version = "0.8.1"
 edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 0895efb..11f283e 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -3,7 +3,7 @@ use std::io::{Error, ErrorKind};
 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, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}};
 
 pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
-
+    
     let mut liststack: Vec<List> = vec![];
     if input.all_lists {
         let list_ids = lists_get_all_ids(config.clone())?;
@@ -15,7 +15,11 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
         println!("Checking for updates of mods in {}", current.id);
         liststack.push(current)
     }
+    
+    cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await
+}
 
+pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_download: bool, delete_old: bool) -> Result<(), Box<dyn std::error::Error>> {
     for current_list in liststack {
         let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
         
@@ -41,17 +45,17 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
             let version_db_string = project.versions.join("|");
 
             //Adding to stack if not the same versions in the list OR if clean == true
-            if input.clean || (version_db_string != current_version.versions) {
-                updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project.clone()).await {
+            if clean || (version_db_string != current_version.versions) {
+                updatestack.push(match specific_update(config.clone(), clean, current_list.clone(), project.clone()).await {
                     Ok(ver) => {
                         current_versions.push((disable_version, p_id));
                         ver
                     },
                     //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE")
-                    Err(e) => {
+                    Err(..) => {
                         //Updating versions in modlist for no repeating version calls
                         mods_change_versions(config.clone(), version_db_string, project.id)?;
-                        println!("({}) No new version found for the specified minecraft version({})", project.title, e);
+                        println!("({}) No new version found for the specified", project.title);
                         continue;
                     },
                 });
@@ -60,7 +64,7 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
             };
         };
 
-        if input.clean {
+        if clean {
             let dl_path = &current_list.download_folder;
             println!("Cleaning {}", dl_path);
             for entry in std::fs::read_dir(dl_path)? {
@@ -69,12 +73,12 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
             }
         }
         
-        if input.direct_download {
+        if direct_download {
             download_versions(current_list.clone(), updatestack).await?;
 
             //Disable old versions
             for ver in current_versions {
-                if input.delete_old {
+                if delete_old {
                     println!("Deleting version {} for mod {}", ver.0, ver.1);
                     delete_version(current_list.clone(), ver.0)?;
                 } else if ver.0 != "NONE" { 
@@ -89,7 +93,7 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
     Ok(())
 }
 
-async fn specific_update(config: Cfg, input: Input, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> {
+async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> {
     println!("Checking update for '{}' in {}", project.title, list.id);
     
     let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;
@@ -106,7 +110,7 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project
 
     
     let mut current: Vec<Version> = vec![];
-    if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) {
+    if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) {
         //get new versions
         print!(" | getting new version");
         let current_str = extract_current_version(applicable_versions.clone())?;
-- 
cgit v1.2.3