summaryrefslogtreecommitdiff
path: root/src/commands/list.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/list.rs')
-rw-r--r--src/commands/list.rs93
1 files changed, 31 insertions, 62 deletions
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 @@
1use std::io::{Error, ErrorKind}; 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
3use 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)]
6pub struct List { 4pub struct List {
@@ -10,32 +8,20 @@ pub struct List {
10 pub download_folder: String, 8 pub download_folder: String,
11} 9}
12 10
13pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 11pub 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
48fn add(config: Cfg, args: Vec<String>) -> MLE<()> { 34fn 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
63fn change(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { 42fn 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
78fn remove(config: Cfg, args: Vec<String>) -> MLE<()> { 47fn 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
91async fn version(config: Cfg, args: Option<Vec<String>>, version_type: Option<MCVersionType>) -> MLE<()> { 56async 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(&current_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}