diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/download.rs | 22 | ||||
-rw-r--r-- | src/commands/io.rs | 30 | ||||
-rw-r--r-- | src/commands/list.rs | 93 | ||||
-rw-r--r-- | src/commands/mod.rs | 4 | ||||
-rw-r--r-- | src/commands/modification.rs | 49 | ||||
-rw-r--r-- | src/commands/setup.rs | 2 | ||||
-rw-r--r-- | src/commands/update.rs | 87 |
7 files changed, 123 insertions, 164 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index b958bf3..7748d15 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use 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}; | 1 | use 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}}; |
2 | use crate::{List, get_current_list, config::Cfg, input::Input}; | 2 | use crate::{List, get_current_list, config::Cfg, input::Input}; |
3 | 3 | ||
4 | pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 4 | pub async fn download(config: Cfg, input: Input) -> MLE<()> { |
5 | 5 | ||
6 | let mut liststack: Vec<List> = vec![]; | 6 | let mut liststack: Vec<List> = vec![]; |
7 | if input.all_lists { | 7 | if input.all_lists { |
@@ -18,7 +18,10 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::erro | |||
18 | for current_list in liststack { | 18 | for current_list in liststack { |
19 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; | 19 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; |
20 | println!("To download: {:#?}", downloaded_versions); | 20 | println!("To download: {:#?}", downloaded_versions); |
21 | let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; | 21 | let current_version_ids = match userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id)) { |
22 | Ok(i) => Ok(i), | ||
23 | Err(e) => Err(MLError::new(ErrorType::DBError, e.to_string().as_str())), | ||
24 | }?; | ||
22 | 25 | ||
23 | let mut to_download: Vec<String> = vec![]; | 26 | let mut to_download: Vec<String> = vec![]; |
24 | //(mod_id, version_id) | 27 | //(mod_id, version_id) |
@@ -33,7 +36,7 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::erro | |||
33 | if current_download.is_none() || input.clean { | 36 | if current_download.is_none() || input.clean { |
34 | to_download.push(current_version); | 37 | to_download.push(current_version); |
35 | } else { | 38 | } else { |
36 | let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; | 39 | let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG").unwrap(); |
37 | if ¤t_version != downloaded_version { | 40 | if ¤t_version != downloaded_version { |
38 | to_disable.push((mod_id.clone(), String::from(downloaded_version))); | 41 | to_disable.push((mod_id.clone(), String::from(downloaded_version))); |
39 | to_download.push(current_version); | 42 | to_download.push(current_version); |
@@ -41,17 +44,10 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::erro | |||
41 | } | 44 | } |
42 | } | 45 | } |
43 | 46 | ||
44 | if input.clean { | 47 | if input.clean { clean_list_dir(¤t_list)? }; |
45 | let dl_path = ¤t_list.download_folder; | ||
46 | println!("Cleaning {}", dl_path); | ||
47 | for entry in std::fs::read_dir(dl_path)? { | ||
48 | let entry = entry?; | ||
49 | std::fs::remove_file(entry.path())?; | ||
50 | } | ||
51 | } | ||
52 | 48 | ||
53 | if !to_download.is_empty() { | 49 | if !to_download.is_empty() { |
54 | 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?; |
55 | } else { | 51 | } else { |
56 | println!("There are no new versions to download"); | 52 | println!("There are no new versions to download"); |
57 | } | 53 | } |
diff --git a/src/commands/io.rs b/src/commands/io.rs index 6c4a4d3..4835e3d 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs | |||
@@ -2,7 +2,7 @@ use std::fs::File; | |||
2 | use std::io::prelude::*; | 2 | use std::io::prelude::*; |
3 | use serde::{Serialize, Deserialize}; | 3 | use serde::{Serialize, Deserialize}; |
4 | 4 | ||
5 | use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List, devdir}; | 5 | use crate::{input::{Input, IoOptions}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, /*mod_add,*/ List, devdir, error::MLE}; |
6 | 6 | ||
7 | #[derive(Debug, Serialize, Deserialize)] | 7 | #[derive(Debug, Serialize, Deserialize)] |
8 | struct Export { | 8 | struct Export { |
@@ -19,7 +19,7 @@ struct ExportList { | |||
19 | } | 19 | } |
20 | 20 | ||
21 | impl ExportList { | 21 | impl ExportList { |
22 | pub fn from(config: Cfg, list_id: String, download: bool) -> Result<Self, Box<dyn std::error::Error>> { | 22 | pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> { |
23 | 23 | ||
24 | let list = lists_get(config.clone(), String::from(&list_id))?; | 24 | let list = lists_get(config.clone(), String::from(&list_id))?; |
25 | 25 | ||
@@ -32,26 +32,22 @@ impl ExportList { | |||
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
35 | pub async fn io(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 35 | pub async fn io(config: Cfg, input: Input) -> MLE<()> { |
36 | 36 | ||
37 | match input.subcommand.clone().ok_or("INVALID_INPUT")? { | 37 | match input.clone().io_options.unwrap() { |
38 | Subcmd::Export => { export(config, input)? }, | 38 | IoOptions::Export => { export(config, input)? }, |
39 | Subcmd::Import => { import(config, input.args).await? }, | 39 | IoOptions::Import => { import(config, input).await? }, |
40 | _ => { }, | ||
41 | } | 40 | } |
42 | 41 | ||
43 | Ok(()) | 42 | Ok(()) |
44 | } | 43 | } |
45 | 44 | ||
46 | fn export(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 45 | fn export(config: Cfg, input: Input) -> MLE<()> { |
47 | let mut list_ids: Vec<String> = vec![]; | 46 | let mut list_ids: Vec<String> = vec![]; |
48 | if input.all_lists { | 47 | if input.all_lists { |
49 | list_ids = lists_get_all_ids(config.clone())?; | 48 | list_ids = lists_get_all_ids(config.clone())?; |
50 | } else { | 49 | } else { |
51 | let args = input.args.ok_or("NO_ARGS")?; | 50 | list_ids.push(lists_get(config.clone(), input.list.unwrap().id)?.id); |
52 | for arg in args { | ||
53 | list_ids.push(lists_get(config.clone(), arg)?.id); | ||
54 | } | ||
55 | } | 51 | } |
56 | let mut lists: Vec<ExportList> = vec![]; | 52 | let mut lists: Vec<ExportList> = vec![]; |
57 | for list_id in list_ids { | 53 | for list_id in list_ids { |
@@ -68,10 +64,10 @@ fn export(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | |||
68 | Ok(()) | 64 | Ok(()) |
69 | } | 65 | } |
70 | 66 | ||
71 | async fn import(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | 67 | async fn import(config: Cfg, input: Input) -> MLE<()> { |
72 | 68 | ||
73 | let filestr: String = match args { | 69 | let filestr: String = match input.file { |
74 | Some(args) => String::from(&args[0]), | 70 | Some(args) => String::from(args), |
75 | None => String::from(devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str())), | 71 | None => String::from(devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str())), |
76 | }; | 72 | }; |
77 | 73 | ||
@@ -83,14 +79,14 @@ async fn import(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn st | |||
83 | println!("{:#?}", export); | 79 | println!("{:#?}", export); |
84 | 80 | ||
85 | for exportlist in export.lists { | 81 | for exportlist in export.lists { |
86 | let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL")? }; | 82 | let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap() }; |
87 | lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; | 83 | lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; |
88 | let mods: Vec<&str> = exportlist.mods.split("|").collect(); | 84 | let mods: Vec<&str> = exportlist.mods.split("|").collect(); |
89 | let mut mod_ids = vec![]; | 85 | let mut mod_ids = vec![]; |
90 | for mod_id in mods { | 86 | for mod_id in mods { |
91 | mod_ids.push(String::from(mod_id)); | 87 | mod_ids.push(String::from(mod_id)); |
92 | }; | 88 | }; |
93 | mod_add(config.clone(), mod_ids, list.clone(), false).await?; | 89 | //mod_add(config.clone(), mod_ids, list.clone(), false).await?; |
94 | } | 90 | } |
95 | Ok(()) | 91 | Ok(()) |
96 | } | 92 | } |
diff --git a/src/commands/list.rs b/src/commands/list.rs index 3998cce..eaf6fa1 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs | |||
@@ -1,6 +1,4 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use 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 | |||
3 | use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}, modrinth::MCVersionType}; | ||
4 | 2 | ||
5 | #[derive(Debug, Clone, PartialEq, Eq)] | 3 | #[derive(Debug, Clone, PartialEq, Eq)] |
6 | pub struct List { | 4 | pub struct List { |
@@ -10,32 +8,20 @@ pub struct List { | |||
10 | pub download_folder: String, | 8 | pub download_folder: String, |
11 | } | 9 | } |
12 | 10 | ||
13 | pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 11 | pub async fn list(config: Cfg, input: Input) -> MLE<()> { |
14 | 12 | ||
15 | match input.subcommand.ok_or("")? { | 13 | match input.clone().list_options.unwrap() { |
16 | Subcmd::Add => { | 14 | ListOptions::Add => { |
17 | match add(config, input.args.ok_or("")?) { | 15 | add(config, input) |
18 | Ok(..) => Ok(()), | ||
19 | Err(e) => Err(Box::new(e)) | ||
20 | } | ||
21 | }, | 16 | }, |
22 | Subcmd::Change => { | 17 | ListOptions::Change => { |
23 | change(config, input.args) | 18 | change(config, input) |
24 | }, | 19 | }, |
25 | Subcmd::Remove => { | 20 | ListOptions::Remove => { |
26 | match remove(config, input.args.ok_or("")?) { | 21 | remove(config, input) |
27 | Ok(..) => Ok(()), | ||
28 | Err(e) => Err(Box::new(e)) | ||
29 | } | ||
30 | }, | 22 | }, |
31 | Subcmd::Version => { | 23 | ListOptions::Version => { |
32 | match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { | 24 | version(config, input).await |
33 | Ok(..) => Ok(()), | ||
34 | Err(e) => Err(Box::new(e)) | ||
35 | } | ||
36 | } | ||
37 | _ => { | ||
38 | Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) | ||
39 | } | 25 | } |
40 | } | 26 | } |
41 | } | 27 | } |
@@ -45,42 +31,21 @@ pub fn get_current_list(config: Cfg) -> MLE<List> { | |||
45 | lists_get(config, id) | 31 | lists_get(config, id) |
46 | } | 32 | } |
47 | 33 | ||
48 | fn add(config: Cfg, args: Vec<String>) -> MLE<()> { | 34 | fn add(config: Cfg, input: Input) -> MLE<()> { |
49 | match args.len() { | 35 | let id = input.list_id.unwrap(); |
50 | 1 | 2 | 3 => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_FEW_ARGUMENTS")), | 36 | let mc_version = input.list_mcversion.unwrap(); |
51 | 4 => { | 37 | let mod_loader = input.modloader.unwrap(); |
52 | let id = String::from(&args[0]); | 38 | let download_folder = input.directory.unwrap(); |
53 | let mc_version = String::from(&args[1]); | 39 | lists_insert(config, id, mc_version, mod_loader, download_folder) |
54 | let mod_loader = Modloader::from(&args[2])?; | ||
55 | let download_folder = String::from(&args[3]); | ||
56 | lists_insert(config, id, mc_version, mod_loader, download_folder) | ||
57 | }, | ||
58 | 5.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")), | ||
59 | _ => panic!("list arguments should never be zero or lower"), | ||
60 | } | ||
61 | } | 40 | } |
62 | 41 | ||
63 | fn change(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | 42 | fn change(config: Cfg, input: Input) -> MLE<()> { |
64 | let lists = lists_get_all_ids(config.clone())?; | 43 | println!("Change default list to: {}", input.clone().list.unwrap().id); |
65 | if args.is_none() { println!("Currently selected list: {}", get_current_list(config)?.id); return Ok(()) }; | 44 | config_change_current_list(config, input.list.unwrap().id) |
66 | let argsvec = args.ok_or("BAH")?; | ||
67 | match argsvec.len() { | ||
68 | 1 => { | ||
69 | let list = String::from(&argsvec[0]); | ||
70 | if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; | ||
71 | config_change_current_list(config, list) | ||
72 | }, | ||
73 | 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), | ||
74 | _ => panic!("list arguments should never lower than zero"), | ||
75 | } | ||
76 | } | 45 | } |
77 | 46 | ||
78 | fn remove(config: Cfg, args: Vec<String>) -> MLE<()> { | 47 | fn remove(config: Cfg, input: Input) -> MLE<()> { |
79 | match args.len() { | 48 | lists_remove(config, input.list.unwrap().id) |
80 | 1 => lists_remove(config, String::from(&args[0])), | ||
81 | 2.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")), | ||
82 | _ => panic!("list arguments should never be zero or lower"), | ||
83 | } | ||
84 | } | 49 | } |
85 | 50 | ||
86 | ///Changing the current lists version and updating it | 51 | ///Changing the current lists version and updating it |
@@ -88,10 +53,14 @@ fn remove(config: Cfg, args: Vec<String>) -> MLE<()> { | |||
88 | /// | 53 | /// |
89 | /// * `config` - The current config | 54 | /// * `config` - The current config |
90 | /// * `args` - All args, to extract the new version | 55 | /// * `args` - All args, to extract the new version |
91 | async fn version(config: Cfg, args: Option<Vec<String>>, version_type: Option<MCVersionType>) -> MLE<()> { | 56 | async fn version(config: Cfg, input: Input) -> MLE<()> { |
92 | 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()); |
58 | |||
59 | lists_version(config.clone(), input.clone().list.ok_or("").unwrap().id, input.clone().list_mcversion.ok_or("").unwrap())?; | ||
60 | |||
61 | //Linebreak readability | ||
62 | println!(""); | ||
93 | 63 | ||
94 | lists_version(config.clone(), String::from(¤t_list.id), String::from(&args.unwrap()[0]))?; | 64 | println!("Check for updates for new minecraft version in list {}", input.clone().list.unwrap().id); |
95 | //update the list & with -- args | 65 | cmd_update(config, vec![input.list.ok_or("").unwrap()], true, input.direct_download, input.delete_old).await |
96 | cmd_update(config, vec![current_list], true, true, false).await | ||
97 | } | 66 | } |
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0d5bd00..38139f9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs | |||
@@ -1,13 +1,13 @@ | |||
1 | pub mod modification; | 1 | pub mod modification; |
2 | pub mod list; | 2 | pub mod list; |
3 | pub mod update; | 3 | pub mod update; |
4 | pub mod setup; | 4 | //pub mod setup; |
5 | pub mod download; | 5 | pub mod download; |
6 | pub mod io; | 6 | pub mod io; |
7 | 7 | ||
8 | pub use modification::*; | 8 | pub use modification::*; |
9 | pub use list::*; | 9 | pub use list::*; |
10 | pub use update::*; | 10 | pub use update::*; |
11 | pub use setup::*; | 11 | //pub use setup::*; |
12 | pub use download::*; | 12 | pub use download::*; |
13 | pub use io::*; | 13 | pub use io::*; |
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 7d4be8d..c82d6b5 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,34 +1,26 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use 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 | ||
3 | use 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}; | 3 | pub async fn modification(config: Cfg, input: Input) -> MLE<()> { |
4 | 4 | match input.clone().mod_options.ok_or("").unwrap() { | |
5 | pub 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 | ||
17 | async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 14 | async 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 | ||
30 | pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { | 21 | pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, direct_download: bool) -> MLE<()> { |
31 | 22 | ||
23 | //Fix printing (its horrible) | ||
32 | println!("Adding mod(s) {:?}", mod_id); | 24 | println!("Adding mod(s) {:?}", mod_id); |
33 | let projects = if mod_id.len() == 1 { | 25 | let projects = if mod_id.len() == 1 { |
34 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] | 26 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] |
@@ -50,7 +42,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl | |||
50 | 42 | ||
51 | current_version_id = current_version.clone().unwrap().id; | 43 | current_version_id = current_version.clone().unwrap().id; |
52 | 44 | ||
53 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | 45 | file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; |
54 | 46 | ||
55 | for ver in available_versions { | 47 | for ver in available_versions { |
56 | available_versions_vec.push(ver.id); | 48 | available_versions_vec.push(ver.id); |
@@ -67,7 +59,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) { | 59 | match userlist_get_all_ids(config.clone(), list.clone().id) { |
68 | Ok(mods) => { | 60 | Ok(mods) => { |
69 | if mods.contains(&project.id) { | 61 | if mods.contains(&project.id) { |
70 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | 62 | return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } |
71 | else { | 63 | else { |
72 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | 64 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; |
73 | } | 65 | } |
@@ -88,24 +80,23 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_downl | |||
88 | }, | 80 | }, |
89 | }; | 81 | }; |
90 | 82 | ||
91 | if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; | 83 | if direct_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; |
92 | 84 | ||
93 | } | 85 | } |
94 | 86 | ||
95 | Ok(()) | 87 | Ok(()) |
96 | } | 88 | } |
97 | 89 | ||
98 | fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { | 90 | fn remove(config: Cfg, input: Input) -> MLE<()> { |
99 | if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; | 91 | |
100 | 92 | //TODO inplement deletion by slug or title | |
101 | let current_list = get_current_list(config.clone())?; | 93 | 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 | 94 | ||
104 | let version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; | 95 | let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; |
105 | 96 | ||
106 | //TODO implement remove from modlist if not in any other lists && config clean is true | 97 | //TODO implement remove from modlist if not in any other lists && config clean is true |
107 | userlist_remove(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; | 98 | userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; |
108 | delete_version(current_list, version)?; | 99 | delete_version(input.list.unwrap(), version)?; |
109 | 100 | ||
110 | let list_ids = lists_get_all_ids(config.clone())?; | 101 | let list_ids = lists_get_all_ids(config.clone())?; |
111 | 102 | ||
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 ca28130..d400a24 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -1,9 +1,6 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | 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, clean_list_dir}, error::{MLE, MLError, ErrorType}}; |
2 | |||
3 | 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}, error::{MLE, MLError, ErrorType}}; | ||
4 | 2 | ||
5 | pub async fn update(config: Cfg, input: Input) -> MLE<()> { | 3 | pub async fn update(config: Cfg, input: Input) -> MLE<()> { |
6 | |||
7 | let mut liststack: Vec<List> = vec![]; | 4 | let mut liststack: Vec<List> = vec![]; |
8 | if input.all_lists { | 5 | if input.all_lists { |
9 | let list_ids = lists_get_all_ids(config.clone())?; | 6 | let list_ids = lists_get_all_ids(config.clone())?; |
@@ -12,10 +9,9 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> { | |||
12 | } | 9 | } |
13 | } else { | 10 | } else { |
14 | let current = get_current_list(config.clone())?; | 11 | let current = get_current_list(config.clone())?; |
15 | println!("Checking for updates of mods in {}", current.id); | 12 | println!("Check for updates of mods in list {}", current.id); |
16 | liststack.push(current) | 13 | liststack.push(current) |
17 | } | 14 | } |
18 | |||
19 | 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 |
20 | } | 16 | } |
21 | 17 | ||
@@ -31,6 +27,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d | |||
31 | let mut projects = projects(String::from(&config.apis.modrinth), mods).await; | 27 | let mut projects = projects(String::from(&config.apis.modrinth), mods).await; |
32 | projects.sort_by_key(|pro| pro.id.clone()); | 28 | projects.sort_by_key(|pro| pro.id.clone()); |
33 | 29 | ||
30 | println!("Comparing mod versions:"); | ||
34 | let mut updatestack: Vec<Version> = vec![]; | 31 | let mut updatestack: Vec<Version> = vec![]; |
35 | for (index, project) in projects.into_iter().enumerate() { | 32 | for (index, project) in projects.into_iter().enumerate() { |
36 | //Get versions for project and check if they match up | 33 | //Get versions for project and check if they match up |
@@ -39,6 +36,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d | |||
39 | let v_id = ¤t_version.mod_id; | 36 | let v_id = ¤t_version.mod_id; |
40 | 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")) }; |
41 | 38 | ||
39 | println!("\t({}) Check for update", project.title); | ||
40 | |||
42 | //Getting current installed version for disable or delete | 41 | //Getting current installed version for disable or delete |
43 | let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&project.id))?; | 42 | let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&project.id))?; |
44 | 43 | ||
@@ -51,51 +50,52 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d | |||
51 | current_versions.push((disable_version, p_id)); | 50 | current_versions.push((disable_version, p_id)); |
52 | ver | 51 | ver |
53 | }, | 52 | }, |
54 | //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") | 53 | Err(e) => { |
55 | Err(..) => { | 54 | //Catch no update available |
56 | //Updating versions in modlist for no repeating version calls | 55 | if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { |
57 | mods_change_versions(config.clone(), version_db_string, project.id)?; | 56 | mods_change_versions(config.clone(), version_db_string, project.id)?; |
58 | 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 | }; | ||
59 | continue; | 61 | continue; |
60 | }, | 62 | }, |
61 | }); | 63 | }); |
62 | } else { | 64 | } else { |
63 | println!("({}) No new version found", project.title); | 65 | println!("\t └No new version found"); |
64 | }; | 66 | }; |
65 | }; | 67 | }; |
68 | |||
69 | //Linebreak readability | ||
70 | println!(""); | ||
66 | 71 | ||
67 | if clean { | 72 | if clean { clean_list_dir(¤t_list)? }; |
68 | let dl_path = ¤t_list.download_folder; | ||
69 | println!("Cleaning {}", dl_path); | ||
70 | for entry in std::fs::read_dir(dl_path)? { | ||
71 | let entry = entry?; | ||
72 | std::fs::remove_file(entry.path())?; | ||
73 | } | ||
74 | } | ||
75 | 73 | ||
76 | if direct_download { | 74 | //Linebreak readability |
77 | download_versions(current_list.clone(), updatestack).await?; | 75 | println!(""); |
76 | |||
77 | if direct_download && !updatestack.is_empty() { | ||
78 | download_versions(current_list.clone(), config.clone(), updatestack).await?; | ||
78 | 79 | ||
79 | //Disable old versions | 80 | //Disable old versions |
80 | for ver in current_versions { | 81 | if !clean { |
81 | if delete_old { | 82 | for ver in current_versions { |
82 | println!("Deleting version {} for mod {}", ver.0, ver.1); | 83 | if delete_old { |
83 | delete_version(current_list.clone(), ver.0)?; | 84 | println!("Deleting version {} for mod {}", ver.0, ver.1); |
84 | } else if ver.0 != "NONE" { | 85 | delete_version(current_list.clone(), ver.0)?; |
85 | println!("Disabling version {} for mod {}", ver.0, ver.1); | 86 | } else if ver.0 != "NONE" { |
86 | disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; | 87 | println!("Disabling version {} for mod {}", ver.0, ver.1); |
87 | }; | 88 | disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; |
89 | }; | ||
90 | } | ||
88 | } | 91 | } |
89 | }; | 92 | }; |
90 | |||
91 | } | 93 | } |
92 | 94 | ||
93 | Ok(()) | 95 | Ok(()) |
94 | } | 96 | } |
95 | 97 | ||
96 | async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> { | 98 | async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> { |
97 | println!("Checking update for '{}' in {}", project.title, list.id); | ||
98 | |||
99 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | 99 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
100 | 100 | ||
101 | let mut versions: Vec<String> = vec![]; | 101 | let mut versions: Vec<String> = vec![]; |
@@ -112,18 +112,24 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) | |||
112 | let mut current: Vec<Version> = vec![]; | 112 | let mut current: Vec<Version> = vec![]; |
113 | if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { | 113 | if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { |
114 | //get new versions | 114 | //get new versions |
115 | print!(" | getting new version"); | 115 | println!("\t └Get versions for specified minecraft versions"); |
116 | let current_str = extract_current_version(applicable_versions.clone())?; | 116 | let current_str = extract_current_version(applicable_versions.clone())?; |
117 | let current_ver = applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("")?; | 117 | let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { |
118 | Ok(v) => Ok(v), | ||
119 | Err(e) => Err(MLError::new(ErrorType::Other, e)), | ||
120 | }?; | ||
118 | current.push(current_ver.clone()); | 121 | current.push(current_ver.clone()); |
119 | 122 | ||
120 | let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url; | 123 | let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { |
124 | Ok(p) => Ok(p), | ||
125 | Err(e) => Err(MLError::new(ErrorType::Other, e)), | ||
126 | }?.url; | ||
121 | userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; | 127 | userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; |
122 | } | 128 | } |
123 | 129 | ||
124 | if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; | 130 | if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; |
125 | 131 | ||
126 | println!(" | ✔️"); | 132 | //println!(" ������️"); |
127 | Ok(current[0].clone()) | 133 | Ok(current[0].clone()) |
128 | } | 134 | } |
129 | 135 | ||
@@ -132,6 +138,7 @@ async fn download_updates_test() { | |||
132 | 138 | ||
133 | use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; | 139 | use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; |
134 | 140 | ||
141 | let config = Cfg::init("modlist.toml").unwrap(); | ||
135 | let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; | 142 | let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; |
136 | 143 | ||
137 | let versions = vec![Version { | 144 | let versions = vec![Version { |
@@ -164,5 +171,5 @@ async fn download_updates_test() { | |||
164 | "fabric".to_string() | 171 | "fabric".to_string() |
165 | ] | 172 | ] |
166 | }]; | 173 | }]; |
167 | assert!(download_versions(current_list, versions).await.is_ok()) | 174 | assert!(download_versions(current_list, config, versions).await.is_ok()) |
168 | } | 175 | } |