From fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 17 Nov 2022 21:20:09 +0100 Subject: added --clean for update && list downloadfolder --- src/commands/list.rs | 58 ++++++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 40 deletions(-) (limited to 'src/commands/list.rs') diff --git a/src/commands/list.rs b/src/commands/list.rs index 6c80e4e..76965df 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,38 +1,27 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::Input}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::{Input, Subcmd}}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { pub id: String, pub mc_version: String, pub modloader: Modloader, + pub download_folder: String, } -pub fn list(config: Cfg, args: Option>) -> Result<(), Box> { +pub fn list(config: Cfg, input: Input) -> Result<(), Box> { - if args.is_none() { - let lists = lists_get_all_ids(config.clone())?; - let current_list = config_get_current_list(config)?; - println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); - return Ok(()); - } - - let arguments = Input::from(args.unwrap().join(" "))?; - - if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - match arguments.command.as_str() { - "add" => { - add(config, arguments.args.unwrap()) + match input.subcommand.ok_or("")? { + Subcmd::Add => { + add(config, input.args.ok_or("")?) }, - "change" => { - change(config, arguments.args.unwrap()) + Subcmd::Change => { + change(config, input.args.ok_or("")?) }, - "remove" => { - remove(config, arguments.args.unwrap()) - }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + Subcmd::Remove => { + remove(config, input.args.ok_or("")?) + } } } @@ -43,16 +32,13 @@ pub fn get_current_list(config: Cfg) -> Result> fn add(config: Cfg, args: Vec) -> Result<(), Box> { match args.len() { - 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), - 3 => { + 1 | 2 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), + 4 => { let id = String::from(&args[0]); let mc_version = String::from(&args[1]); - let mod_loader = match args[2].as_str() { - "forge" => Modloader::Forge, - "fabric" => Modloader::Fabric, - _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) - }; - lists_insert(config, id, mc_version, mod_loader) + let mod_loader = Modloader::from(&args[2])?; + let download_folder = String::from(&args[3]); + lists_insert(config, id, mc_version, mod_loader, download_folder) }, 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), @@ -65,10 +51,7 @@ fn change(config: Cfg, args: Vec) -> Result<(), Box { let list = String::from(&args[0]); if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; - match config_change_current_list(config, list) { - Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) }, - Ok(()) => Ok(()), - } + config_change_current_list(config, list) }, 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), @@ -77,12 +60,7 @@ fn change(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box> { match args.len() { - 1 => { - match lists_remove(config, String::from(&args[0])) { - Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) }, - Ok(()) => Ok(()), - } - }, + 1 => lists_remove(config, String::from(&args[0])), 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), } -- cgit v1.2.3