From 57ab6addda10a49c18dc09208dfb319c0205e869 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 26 Jan 2023 17:23:06 +0100 Subject: Todos; fixed input with "-" --- src/input.rs | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'src/input.rs') diff --git a/src/input.rs b/src/input.rs index a41f671..144f22a 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,4 @@ -use crate::{error::{MLE, MLError, ErrorType}, Modloader, config::Cfg, db::lists_get, get_current_list, List}; +use crate::{error::{MLE, MLError, ErrorType}, Modloader, config::Cfg, db::lists_get, get_current_list, List, modrinth::{get_minecraft_version, MCVersionType}}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Input { @@ -54,10 +54,8 @@ pub enum IoOptions { impl Input { fn from(config: Cfg, input: Vec) -> MLE { let input_string = input.join(" "); - let mut args: Vec<&str> = input_string.split('-').collect(); - args.reverse(); - args.pop(); - args.reverse(); + let mut args: Vec<&str> = input_string.split(" -").collect(); + args[0] = args[0].split_at(1).1; let mut command: Option = None; @@ -95,6 +93,8 @@ impl Input { mod_options = Some(ModOptions::Add); if arg_split.len() == 2 { mod_id = Some(String::from(arg_split[1])); + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please specify a list mod slug or id")); } }, "mv" => { @@ -102,6 +102,8 @@ impl Input { mod_options = Some(ModOptions::Add); if arg_split.len() == 2 { mod_version = Some(String::from(arg_split[1])); + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please specify a version id")); }; }, "mr" => { @@ -109,7 +111,9 @@ impl Input { mod_options = Some(ModOptions::Remove); if arg_split.len() == 2 { mod_id = Some(String::from(arg_split[1])); - } + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please specify a mod id")); + }; }, "set_version" => { set_version = true; @@ -120,7 +124,7 @@ impl Input { "clean" => { clean = true; }, - "no-download" => { + "no_download" => { direct_download = false; }, "delete_old" => { @@ -136,7 +140,11 @@ impl Input { "la" => { command = Some(Cmd::List); list_options = Some(ListOptions::Add); - list_id = Some(String::from(arg_split[1])); + if arg_split.len() == 2 { + list_id = Some(String::from(arg_split[1])); + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please give the new list an id")); + } }, "lr" => { command = Some(Cmd::List); @@ -163,10 +171,18 @@ impl Input { } }, "ml" => { - modloader = Some(Modloader::from(arg_split[1])?); + if arg_split.len() == 2 { + modloader = Some(Modloader::from(arg_split[1])?); + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please specify a modloader")); + } }, "dir" => { - directory = Some(String::from(arg_split[1])); + if arg_split.len() == 2 { + directory = Some(String::from(arg_split[1])); + } else { + return Err(MLError::new(ErrorType::ArgumentError, "Please specify a directory")); + } }, "export" => { command = Some(Cmd::Io); @@ -212,18 +228,16 @@ pub async fn get_input(config: Cfg, args: Vec) -> MLE { match input.clone().command.unwrap() { Cmd::Mod => check_mod(input, config), - Cmd::List => check_list(input, config), + Cmd::List => check_list(input, config).await, _ => Ok(input), } } -//Move checks to commands? translate to variables there? fn check_mod(mut input: Input, config: Cfg) -> MLE { if input.mod_options.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No mod option")); }; match input.clone().mod_options.unwrap() { - //Check for MV if no mod-id on both ModOptions::Add => { if input.mod_id.is_none() && input.mod_version.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No mod id/slug or version id")); }; if input.list_id.is_none() { input.list = Some(get_current_list(config.clone())?); }; @@ -236,7 +250,7 @@ fn check_mod(mut input: Input, config: Cfg) -> MLE { } } -fn check_list(mut input: Input, config: Cfg) -> MLE { +async fn check_list(mut input: Input, config: Cfg) -> MLE { if input.list_options.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_ARGUMENT")); }; @@ -245,11 +259,14 @@ fn check_list(mut input: Input, config: Cfg) -> MLE { if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no list id specified")); }; if input.list_mcversion.is_none() { println!("No Minecraft Version specified, defaulting to latest release"); - //TODO Get latest version - input.list_mcversion = Some(String::from("1.19.3")); + input.list_mcversion = Some(get_minecraft_version(config.apis.modrinth, MCVersionType::Release).await); + }; + if input.directory.is_none() { + let id = input.clone().list_id.unwrap(); + println!("No download directory specified, defaulting to ./downloads/{}", id); + input.directory = Some(format!("./downloads/{}", id)) }; if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no modloader specified")); }; - if input.directory.is_none() { input.directory = Some(format!("./downloads/{}", input.clone().list_id.expect("earlier if failed"))) }; Ok(input) }, ListOptions::Remove => { -- cgit v1.2.3