From 1890d59428dfcca861ea1b7820411d80cc60d713 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 22 Jan 2023 22:34:17 +0100 Subject: Added list version cmd, fixed some todos --- src/commands/modification.rs | 45 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 7d4be8d..6a03b35 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,33 +1,24 @@ -use std::io::{Error, ErrorKind}; +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}}; -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}; - -pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.as_ref().ok_or("")? { - Subcmd::Add => { +pub async fn modification(config: Cfg, input: Input) -> MLE<()> { + match input.clone().mod_options.ok_or("").unwrap() { + ModOptions::Add => { add(config, input).await }, - Subcmd::Remove => { - remove(config, input.args.ok_or("")?) + ModOptions::Remove => { + remove(config, input) }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE"))) } } -async fn add(config: Cfg, input: Input) -> Result<(), Box> { - - let args = input.args.ok_or("")?; +async fn add(config: Cfg, input: Input) -> MLE<()> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - let current_list = get_current_list(config.clone())?; - - mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; + mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?; Ok(()) } -pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> Result<(), Box> { +pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> MLE<()> { println!("Adding mod(s) {:?}", mod_id); let projects = if mod_id.len() == 1 { @@ -50,7 +41,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl current_version_id = current_version.clone().unwrap().id; - file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; + file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; for ver in available_versions { available_versions_vec.push(ver.id); @@ -67,7 +58,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl match userlist_get_all_ids(config.clone(), list.clone().id) { Ok(mods) => { if mods.contains(&project.id) { - return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } + return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } else { userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; } @@ -88,24 +79,22 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl }, }; - if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; + if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; } Ok(()) } -fn remove(config: Cfg, args: Vec) -> Result<(), Box> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; +fn remove(config: Cfg, input: Input) -> MLE<()> { - let current_list = get_current_list(config.clone())?; - let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?; + let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; - let version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; + let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; //TODO implement remove from modlist if not in any other lists && config clean is true - userlist_remove(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; - delete_version(current_list, version)?; + userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; + delete_version(input.list.unwrap(), version)?; let list_ids = lists_get_all_ids(config.clone())?; -- cgit v1.2.3 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/commands/modification.rs | 4 +++- src/commands/update.rs | 3 ++- src/files.rs | 1 - src/input.rs | 51 +++++++++++++++++++++++++++++--------------- src/main.rs | 4 ++-- 5 files changed, 41 insertions(+), 22 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 6a03b35..c194202 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -20,6 +20,7 @@ async fn add(config: Cfg, input: Input) -> MLE<()> { pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> MLE<()> { + //Fix printing (its horrible) println!("Adding mod(s) {:?}", mod_id); let projects = if mod_id.len() == 1 { vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] @@ -87,7 +88,8 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl } fn remove(config: Cfg, input: Input) -> MLE<()> { - + + //TODO inplement deletion by slug or title let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; diff --git a/src/commands/update.rs b/src/commands/update.rs index f8bdb82..f71f537 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -139,6 +139,7 @@ async fn download_updates_test() { use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; + let config = Cfg::init("modlist.toml").unwrap(); let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; let versions = vec![Version { @@ -171,5 +172,5 @@ async fn download_updates_test() { "fabric".to_string() ] }]; - assert!(download_versions(current_list, versions).await.is_ok()) + assert!(download_versions(current_list, config, versions).await.is_ok()) } diff --git a/src/files.rs b/src/files.rs index 0d7dc0a..ecb6e3e 100644 --- a/src/files.rs +++ b/src/files.rs @@ -11,7 +11,6 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) println!("Download to directory from: {} ({})", list.id, dl_path); for ver in versions { - //TODO get project name instead of projectid from db let project_name = mods_get_name(config.clone(), &ver.project_id)?; print!("\t({})Download version {}", project_name, ver.id); std::io::stdout().flush().unwrap(); 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 => { diff --git a/src/main.rs b/src/main.rs index 2fca691..d177c3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, process}; -use modlist::{config::Cfg, input::{get_input, Cmd}, update, download, list, io}; +use modlist::{config::Cfg, input::{get_input, Cmd}, update, download, list, io, modification}; #[tokio::main] async fn main() { @@ -23,7 +23,7 @@ async fn main() { match input.clone().command.unwrap() { Cmd::Mod => { - Ok(()) + modification(config, input).await }, Cmd::List => { list(config, input).await -- cgit v1.2.3 From 54752f7eb39e20929a7816c90be33bf8dbff2a6c Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 29 Jan 2023 13:56:06 +0100 Subject: fixed not downloading on mod add --- src/commands/modification.rs | 4 ++-- src/commands/update.rs | 3 +-- src/files.rs | 3 ++- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index c194202..c82d6b5 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -18,7 +18,7 @@ async fn add(config: Cfg, input: Input) -> MLE<()> { Ok(()) } -pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> MLE<()> { +pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, direct_download: bool) -> MLE<()> { //Fix printing (its horrible) println!("Adding mod(s) {:?}", mod_id); @@ -80,7 +80,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl }, }; - if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; + if direct_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; } diff --git a/src/commands/update.rs b/src/commands/update.rs index f71f537..d400a24 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -74,7 +74,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d //Linebreak readability println!(""); - if direct_download { + if direct_download && !updatestack.is_empty() { download_versions(current_list.clone(), config.clone(), updatestack).await?; //Disable old versions @@ -90,7 +90,6 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d } } }; - } Ok(()) diff --git a/src/files.rs b/src/files.rs index ecb6e3e..8c822b2 100644 --- a/src/files.rs +++ b/src/files.rs @@ -13,6 +13,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) for ver in versions { let project_name = mods_get_name(config.clone(), &ver.project_id)?; print!("\t({})Download version {}", project_name, ver.id); + //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush().unwrap(); let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); @@ -22,7 +23,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) }; let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); download_file(primary_file.url, list.clone().download_folder, filename).await?; - tokio::time::sleep(std::time::Duration::new(3, 0)).await; + //tokio::time::sleep(std::time::Duration::new(3, 0)).await; println!(" ✓"); } -- cgit v1.2.3