summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/download.rs13
-rw-r--r--src/commands/list.rs30
-rw-r--r--src/commands/mod.rs4
-rw-r--r--src/commands/modification.rs45
-rw-r--r--src/commands/setup.rs2
-rw-r--r--src/commands/update.rs67
6 files changed, 72 insertions, 89 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index 0f63876..7748d15 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -1,4 +1,4 @@
1use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}}; 1use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version, clean_list_dir}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}};
2use crate::{List, get_current_list, config::Cfg, input::Input}; 2use crate::{List, get_current_list, config::Cfg, input::Input};
3 3
4pub async fn download(config: Cfg, input: Input) -> MLE<()> { 4pub async fn download(config: Cfg, input: Input) -> MLE<()> {
@@ -44,17 +44,10 @@ pub async fn download(config: Cfg, input: Input) -> MLE<()> {
44 } 44 }
45 } 45 }
46 46
47 if input.clean { 47 if input.clean { clean_list_dir(&current_list)? };
48 let dl_path = &current_list.download_folder;
49 println!("Cleaning {}", dl_path);
50 for entry in std::fs::read_dir(dl_path)? {
51 let entry = entry?;
52 std::fs::remove_file(entry.path())?;
53 }
54 }
55 48
56 if !to_download.is_empty() { 49 if !to_download.is_empty() {
57 download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; 50 download_versions(current_list.clone(), config.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?;
58 } else { 51 } else {
59 println!("There are no new versions to download"); 52 println!("There are no new versions to download");
60 } 53 }
diff --git a/src/commands/list.rs b/src/commands/list.rs
index bc58787..eaf6fa1 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,4 +1,4 @@
1use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::MLE, /*modrinth::MCVersionType*/}; 1use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, ListOptions}, cmd_update, error::MLE};
2 2
3#[derive(Debug, Clone, PartialEq, Eq)] 3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct List { 4pub struct List {
@@ -20,13 +20,9 @@ pub async fn list(config: Cfg, input: Input) -> MLE<()> {
20 ListOptions::Remove => { 20 ListOptions::Remove => {
21 remove(config, input) 21 remove(config, input)
22 }, 22 },
23 /* 23 ListOptions::Version => {
24 Subcmd::Version => { 24 version(config, input).await
25 match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { 25 }
26 Ok(..) => Ok(()),
27 Err(e) => Err(Box::new(e))
28 }
29 }*/
30 } 26 }
31} 27}
32 28
@@ -44,7 +40,7 @@ fn add(config: Cfg, input: Input) -> MLE<()> {
44} 40}
45 41
46fn change(config: Cfg, input: Input) -> MLE<()> { 42fn change(config: Cfg, input: Input) -> MLE<()> {
47 //TODO reimplement current list 43 println!("Change default list to: {}", input.clone().list.unwrap().id);
48 config_change_current_list(config, input.list.unwrap().id) 44 config_change_current_list(config, input.list.unwrap().id)
49} 45}
50 46
@@ -52,17 +48,19 @@ fn remove(config: Cfg, input: Input) -> MLE<()> {
52 lists_remove(config, input.list.unwrap().id) 48 lists_remove(config, input.list.unwrap().id)
53} 49}
54 50
55/*
56///Changing the current lists version and updating it 51///Changing the current lists version and updating it
57/// #Arguments 52/// #Arguments
58/// 53///
59/// * `config` - The current config 54/// * `config` - The current config
60/// * `args` - All args, to extract the new version 55/// * `args` - All args, to extract the new version
61async fn version(config: Cfg, args: Option<Vec<String>>, version_type: Option<MCVersionType>) -> MLE<()> { 56async fn version(config: Cfg, input: Input) -> MLE<()> {
62 let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?; 57 println!("Change version for list {} to minecraft version: {}", input.clone().list.unwrap().id, input.clone().list_mcversion.unwrap());
63 58
64 lists_version(config.clone(), String::from(&current_list.id), String::from(&args.unwrap()[0]))?; 59 lists_version(config.clone(), input.clone().list.ok_or("").unwrap().id, input.clone().list_mcversion.ok_or("").unwrap())?;
65 //update the list & with -- args 60
66 cmd_update(config, vec![current_list], true, true, false).await 61 //Linebreak readability
62 println!("");
63
64 println!("Check for updates for new minecraft version in list {}", input.clone().list.unwrap().id);
65 cmd_update(config, vec![input.list.ok_or("").unwrap()], true, input.direct_download, input.delete_old).await
67} 66}
68*/
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 527afc7..38139f9 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,11 +1,11 @@
1//pub mod modification; 1pub mod modification;
2pub mod list; 2pub mod list;
3pub mod update; 3pub mod update;
4//pub mod setup; 4//pub mod setup;
5pub mod download; 5pub mod download;
6pub mod io; 6pub mod io;
7 7
8//pub use modification::*; 8pub use modification::*;
9pub use list::*; 9pub use list::*;
10pub use update::*; 10pub use update::*;
11//pub use setup::*; 11//pub use setup::*;
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 7d4be8d..6a03b35 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,33 +1,24 @@
1use std::io::{Error, ErrorKind}; 1use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}};
2 2
3use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; 3pub async fn modification(config: Cfg, input: Input) -> MLE<()> {
4 4 match input.clone().mod_options.ok_or("").unwrap() {
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5 ModOptions::Add => {
6 match input.subcommand.as_ref().ok_or("")? {
7 Subcmd::Add => {
8 add(config, input).await 6 add(config, input).await
9 }, 7 },
10 Subcmd::Remove => { 8 ModOptions::Remove => {
11 remove(config, input.args.ok_or("")?) 9 remove(config, input)
12 }, 10 },
13 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE")))
14 } 11 }
15} 12}
16 13
17async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 14async fn add(config: Cfg, input: Input) -> MLE<()> {
18
19 let args = input.args.ok_or("")?;
20 15
21 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; 16 mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?;
22
23 let current_list = get_current_list(config.clone())?;
24
25 mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?;
26 17
27 Ok(()) 18 Ok(())
28} 19}
29 20
30pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { 21pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> MLE<()> {
31 22
32 println!("Adding mod(s) {:?}", mod_id); 23 println!("Adding mod(s) {:?}", mod_id);
33 let projects = if mod_id.len() == 1 { 24 let projects = if mod_id.len() == 1 {
@@ -50,7 +41,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
50 41
51 current_version_id = current_version.clone().unwrap().id; 42 current_version_id = current_version.clone().unwrap().id;
52 43
53 file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; 44 file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url;
54 45
55 for ver in available_versions { 46 for ver in available_versions {
56 available_versions_vec.push(ver.id); 47 available_versions_vec.push(ver.id);
@@ -67,7 +58,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
67 match userlist_get_all_ids(config.clone(), list.clone().id) { 58 match userlist_get_all_ids(config.clone(), list.clone().id) {
68 Ok(mods) => { 59 Ok(mods) => {
69 if mods.contains(&project.id) { 60 if mods.contains(&project.id) {
70 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 61 return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); }
71 else { 62 else {
72 userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?; 63 userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?;
73 } 64 }
@@ -88,24 +79,22 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl
88 }, 79 },
89 }; 80 };
90 81
91 if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; 82 if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; };
92 83
93 } 84 }
94 85
95 Ok(()) 86 Ok(())
96} 87}
97 88
98fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 89fn remove(config: Cfg, input: Input) -> MLE<()> {
99 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
100 90
101 let current_list = get_current_list(config.clone())?; 91 let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?;
102 let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?;
103 92
104 let version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&mod_id))?; 93 let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?;
105 94
106 //TODO implement remove from modlist if not in any other lists && config clean is true 95 //TODO implement remove from modlist if not in any other lists && config clean is true
107 userlist_remove(config.clone(), String::from(&current_list.id), String::from(&mod_id))?; 96 userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?;
108 delete_version(current_list, version)?; 97 delete_version(input.list.unwrap(), version)?;
109 98
110 let list_ids = lists_get_all_ids(config.clone())?; 99 let list_ids = lists_get_all_ids(config.clone())?;
111 100
diff --git a/src/commands/setup.rs b/src/commands/setup.rs
index e4fa801..cc7472c 100644
--- a/src/commands/setup.rs
+++ b/src/commands/setup.rs
@@ -63,4 +63,4 @@ fn to_04(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
63 s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?; 63 s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?;
64 } 64 }
65 s_config_update_version(config, String::from("0.4")) 65 s_config_update_version(config, String::from("0.4"))
66} 66} \ No newline at end of file
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 068c3f3..f8bdb82 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,7 +1,6 @@
1use 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}, error::{MLE, MLError, ErrorType}}; 1use 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, clean_list_dir}, error::{MLE, MLError, ErrorType}};
2 2
3pub async fn update(config: Cfg, input: Input) -> MLE<()> { 3pub async fn update(config: Cfg, input: Input) -> MLE<()> {
4
5 let mut liststack: Vec<List> = vec![]; 4 let mut liststack: Vec<List> = vec![];
6 if input.all_lists { 5 if input.all_lists {
7 let list_ids = lists_get_all_ids(config.clone())?; 6 let list_ids = lists_get_all_ids(config.clone())?;
@@ -10,10 +9,9 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> {
10 } 9 }
11 } else { 10 } else {
12 let current = get_current_list(config.clone())?; 11 let current = get_current_list(config.clone())?;
13 println!("Checking for updates of mods in {}", current.id); 12 println!("Check for updates of mods in list {}", current.id);
14 liststack.push(current) 13 liststack.push(current)
15 } 14 }
16
17 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await 15 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await
18} 16}
19 17
@@ -29,6 +27,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
29 let mut projects = projects(String::from(&config.apis.modrinth), mods).await; 27 let mut projects = projects(String::from(&config.apis.modrinth), mods).await;
30 projects.sort_by_key(|pro| pro.id.clone()); 28 projects.sort_by_key(|pro| pro.id.clone());
31 29
30 println!("Comparing mod versions:");
32 let mut updatestack: Vec<Version> = vec![]; 31 let mut updatestack: Vec<Version> = vec![];
33 for (index, project) in projects.into_iter().enumerate() { 32 for (index, project) in projects.into_iter().enumerate() {
34 //Get versions for project and check if they match up 33 //Get versions for project and check if they match up
@@ -37,6 +36,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
37 let v_id = &current_version.mod_id; 36 let v_id = &current_version.mod_id;
38 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) }; 37 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) };
39 38
39 println!("\t({}) Check for update", project.title);
40
40 //Getting current installed version for disable or delete 41 //Getting current installed version for disable or delete
41 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?; 42 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?;
42 43
@@ -49,40 +50,44 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
49 current_versions.push((disable_version, p_id)); 50 current_versions.push((disable_version, p_id));
50 ver 51 ver
51 }, 52 },
52 //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") 53 Err(e) => {
53 Err(..) => { 54 //Catch no update available
54 //Updating versions in modlist for no repeating version calls 55 if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" {
55 mods_change_versions(config.clone(), version_db_string, project.id)?; 56 mods_change_versions(config.clone(), version_db_string, project.id)?;
56 println!("({}) No new version found for the specified", project.title); 57 println!("\t └No new version found for the specified minecraft version");
58 } else {
59 return Err(e);
60 };
57 continue; 61 continue;
58 }, 62 },
59 }); 63 });
60 } else { 64 } else {
61 println!("({}) No new version found", project.title); 65 println!("\t No new version found");
62 }; 66 };
63 }; 67 };
68
69 //Linebreak readability
70 println!("");
64 71
65 if clean { 72 if clean { clean_list_dir(&current_list)? };
66 let dl_path = &current_list.download_folder;
67 println!("Cleaning {}", dl_path);
68 for entry in std::fs::read_dir(dl_path)? {
69 let entry = entry?;
70 std::fs::remove_file(entry.path())?;
71 }
72 }
73 73
74 //Linebreak readability
75 println!("");
76
74 if direct_download { 77 if direct_download {
75 download_versions(current_list.clone(), updatestack).await?; 78 download_versions(current_list.clone(), config.clone(), updatestack).await?;
76 79
77 //Disable old versions 80 //Disable old versions
78 for ver in current_versions { 81 if !clean {
79 if delete_old { 82 for ver in current_versions {
80 println!("Deleting version {} for mod {}", ver.0, ver.1); 83 if delete_old {
81 delete_version(current_list.clone(), ver.0)?; 84 println!("Deleting version {} for mod {}", ver.0, ver.1);
82 } else if ver.0 != "NONE" { 85 delete_version(current_list.clone(), ver.0)?;
83 println!("Disabling version {} for mod {}", ver.0, ver.1); 86 } else if ver.0 != "NONE" {
84 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; 87 println!("Disabling version {} for mod {}", ver.0, ver.1);
85 }; 88 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
89 };
90 }
86 } 91 }
87 }; 92 };
88 93
@@ -92,8 +97,6 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
92} 97}
93 98
94async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> { 99async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> {
95 println!("Checking update for '{}' in {}", project.title, list.id);
96
97 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; 100 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;
98 101
99 let mut versions: Vec<String> = vec![]; 102 let mut versions: Vec<String> = vec![];
@@ -110,7 +113,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project)
110 let mut current: Vec<Version> = vec![]; 113 let mut current: Vec<Version> = vec![];
111 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { 114 if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) {
112 //get new versions 115 //get new versions
113 print!(" | getting new version"); 116 println!("\t └Get versions for specified minecraft versions");
114 let current_str = extract_current_version(applicable_versions.clone())?; 117 let current_str = extract_current_version(applicable_versions.clone())?;
115 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { 118 let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") {
116 Ok(v) => Ok(v), 119 Ok(v) => Ok(v),
@@ -122,12 +125,12 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project)
122 Ok(p) => Ok(p), 125 Ok(p) => Ok(p),
123 Err(e) => Err(MLError::new(ErrorType::Other, e)), 126 Err(e) => Err(MLError::new(ErrorType::Other, e)),
124 }?.url; 127 }?.url;
125 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id); 128 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?;
126 } 129 }
127 130
128 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; 131 if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) };
129 132
130 println!(" | ✔️"); 133 //println!(" ������️");
131 Ok(current[0].clone()) 134 Ok(current[0].clone())
132} 135}
133 136