diff options
author | fxqnlr <[email protected]> | 2023-01-19 18:37:42 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-01-19 18:37:42 +0100 |
commit | f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537 (patch) | |
tree | 21d0c7356b55e9b45517fd9ca874dd3270b6bc3a /src/commands | |
parent | be9f74f4fb82b25abd99f7024e0f5eea2691f34f (diff) | |
download | modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.tar modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.tar.gz modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.zip |
input mostly inplemented, mods missing
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/download.rs | 11 | ||||
-rw-r--r-- | src/commands/io.rs | 30 | ||||
-rw-r--r-- | src/commands/list.rs | 73 | ||||
-rw-r--r-- | src/commands/mod.rs | 12 | ||||
-rw-r--r-- | src/commands/update.rs | 18 |
5 files changed, 58 insertions, 86 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs index b958bf3..0f63876 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}, 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); |
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 2fec1c7..bc58787 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}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::MLE, /*modrinth::MCVersionType*/}; |
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, ListOptions}, /*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 { |
@@ -9,78 +7,49 @@ pub struct List { | |||
9 | pub modloader: Modloader, | 7 | pub modloader: Modloader, |
10 | pub download_folder: String, | 8 | pub download_folder: String, |
11 | } | 9 | } |
12 | /* | ||
13 | pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | ||
14 | 10 | ||
15 | match input.list_options.ok_or("")? { | 11 | pub async fn list(config: Cfg, input: Input) -> MLE<()> { |
12 | |||
13 | match input.clone().list_options.unwrap() { | ||
16 | ListOptions::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 | ListOptions::Change => { | 17 | ListOptions::Change => { |
23 | change(config, input.args) | 18 | change(config, input) |
24 | }, | 19 | }, |
25 | ListOptions::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 | }, |
23 | /* | ||
31 | Subcmd::Version => { | 24 | Subcmd::Version => { |
32 | match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { | 25 | match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { |
33 | Ok(..) => Ok(()), | 26 | Ok(..) => Ok(()), |
34 | Err(e) => Err(Box::new(e)) | 27 | Err(e) => Err(Box::new(e)) |
35 | } | 28 | } |
36 | } | 29 | }*/ |
37 | _ => { | ||
38 | Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) | ||
39 | } | ||
40 | } | 30 | } |
41 | } | 31 | } |
42 | */ | 32 | |
43 | pub fn get_current_list(config: Cfg) -> MLE<List> { | 33 | pub fn get_current_list(config: Cfg) -> MLE<List> { |
44 | let id = config_get_current_list(config.clone())?; | 34 | let id = config_get_current_list(config.clone())?; |
45 | lists_get(config, id) | 35 | lists_get(config, id) |
46 | } | 36 | } |
47 | 37 | ||
48 | fn add(config: Cfg, args: Vec<String>) -> MLE<()> { | 38 | fn add(config: Cfg, input: Input) -> MLE<()> { |
49 | match args.len() { | 39 | let id = input.list_id.unwrap(); |
50 | 1 | 2 | 3 => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_FEW_ARGUMENTS")), | 40 | let mc_version = input.list_mcversion.unwrap(); |
51 | 4 => { | 41 | let mod_loader = input.modloader.unwrap(); |
52 | let id = String::from(&args[0]); | 42 | let download_folder = input.directory.unwrap(); |
53 | let mc_version = String::from(&args[1]); | 43 | 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 | } | 44 | } |
62 | 45 | ||
63 | fn change(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | 46 | fn change(config: Cfg, input: Input) -> MLE<()> { |
64 | let lists = lists_get_all_ids(config.clone())?; | 47 | //TODO reimplement current list |
65 | if args.is_none() { println!("Currently selected list: {}", get_current_list(config)?.id); return Ok(()) }; | 48 | 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 | } | 49 | } |
77 | 50 | ||
78 | fn remove(config: Cfg, args: Vec<String>) -> MLE<()> { | 51 | fn remove(config: Cfg, input: Input) -> MLE<()> { |
79 | match args.len() { | 52 | 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 | } | 53 | } |
85 | 54 | ||
86 | /* | 55 | /* |
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 29fc600..527afc7 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/update.rs b/src/commands/update.rs index ca28130..068c3f3 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | use std::io::{Error, ErrorKind}; | ||
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}}; | 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}, 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<()> { |
@@ -93,7 +91,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d | |||
93 | Ok(()) | 91 | Ok(()) |
94 | } | 92 | } |
95 | 93 | ||
96 | async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> Result<Version, Box<dyn std::error::Error>> { | 94 | async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE<Version> { |
97 | println!("Checking update for '{}' in {}", project.title, list.id); | 95 | println!("Checking update for '{}' in {}", project.title, list.id); |
98 | 96 | ||
99 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | 97 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
@@ -114,14 +112,20 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) | |||
114 | //get new versions | 112 | //get new versions |
115 | print!(" | getting new version"); | 113 | print!(" | getting new version"); |
116 | let current_str = extract_current_version(applicable_versions.clone())?; | 114 | 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("")?; | 115 | 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), | ||
117 | Err(e) => Err(MLError::new(ErrorType::Other, e)), | ||
118 | }?; | ||
118 | current.push(current_ver.clone()); | 119 | current.push(current_ver.clone()); |
119 | 120 | ||
120 | let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url; | 121 | let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { |
121 | userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; | 122 | Ok(p) => Ok(p), |
123 | Err(e) => Err(MLError::new(ErrorType::Other, e)), | ||
124 | }?.url; | ||
125 | userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id); | ||
122 | } | 126 | } |
123 | 127 | ||
124 | if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; | 128 | if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; |
125 | 129 | ||
126 | println!(" | ✔️"); | 130 | println!(" | ✔️"); |
127 | Ok(current[0].clone()) | 131 | Ok(current[0].clone()) |