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 ++++++++++++++------------------------------ src/commands/modification.rs | 31 +++++++---------------- src/commands/setup.rs | 11 ++++++--- src/commands/update.rs | 35 +++++++++++++++++--------- 4 files changed, 58 insertions(+), 77 deletions(-) (limited to 'src/commands') 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"), } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 595b677..519a0cb 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,23 +1,17 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::Input, get_current_list}; +use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list}; -pub async fn modification(config: Cfg, args: Option>) -> Result<(), Box> { +pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } - - 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()).await + match input.subcommand.ok_or("")? { + Subcmd::Add => { + add(config, input.args.ok_or("")?).await }, - "remove" => { - remove(config, arguments.args.unwrap()) + Subcmd::Remove => { + remove(config, input.args.ok_or("")?) }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE"))) } } @@ -29,8 +23,6 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box { - dbg!(&mods); if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } else { @@ -60,7 +51,6 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box { - dbg!(&mods); if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) } else { @@ -81,8 +71,5 @@ fn remove(config: Cfg, args: Vec) -> Result<(), Box { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, - Ok(()) => Ok(()), - } + userlist_remove(config, current_list.id, mod_id) } diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 8c0fcfd..be06040 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -1,9 +1,8 @@ use std::{fs::File, path::Path, io::{Error, ErrorKind}}; -use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download}, modrinth::get_raw_versions}; +use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download, s_config_update_version}, modrinth::get_raw_versions}; pub async fn setup(config: Cfg) -> Result<(), Box> { - let db_file = format!("{}/data.db", String::from(&config.data)); if !Path::new(&db_file).exists() { @@ -13,6 +12,7 @@ pub async fn setup(config: Cfg) -> Result<(), Box> { match s_config_get_version(config.clone()) { Ok(ver) => { match ver.as_str() { + "0.2" => to_03(config)?, _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) } }, @@ -33,7 +33,7 @@ async fn to_02(config: Cfg) -> Result<(), Box> { for list in lists { println!("Updating {}", list); - s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?; + s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"))?; let full_list = lists_get(config.clone(), String::from(&list))?; @@ -51,3 +51,8 @@ async fn to_02(config: Cfg) -> Result<(), Box> { Ok(()) } + +fn to_03(config: Cfg) -> Result<(), Box> { + s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"))?; + s_config_update_version(config, String::from("0.3")) +} diff --git a/src/commands/update.rs b/src/commands/update.rs index e383eae..85630f5 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -4,9 +4,9 @@ use reqwest::Client; use futures_util::StreamExt; -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}, List}; +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}, List, input::Input}; -pub async fn update(config: Cfg) -> Result<(), Box> { +pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { let current_list = get_current_list(config.clone())?; @@ -20,15 +20,16 @@ pub async fn update(config: Cfg) -> Result<(), Box> { let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { + //Get versions for project and check if they match up let current_version = &versions[index]; - let p_id = String::from(&project.id); let v_id = ¤t_version.mod_id; - if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; - - if project.versions.join("|") != current_version.versions { - updatestack.push(match specific_update(config.clone(), current_list.clone(), project).await { + + + //Adding to stack if not the same versions in the list OR if clean == true + if input.clone().clean || (project.versions.join("|") != current_version.versions) { + updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project).await { Ok(ver) => ver, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") Err(_) => { continue; }, @@ -36,13 +37,23 @@ pub async fn update(config: Cfg) -> Result<(), Box> { }; }; //println!("{:?}", updatestack); + + + if input.clean { + let dl_path = ¤t_list.download_folder; + println!("Cleaning {}", dl_path); + for entry in std::fs::read_dir(dl_path)? { + let entry = entry?; + std::fs::remove_file(entry.path())?; + } + } - download_updates(config, updatestack).await?; + download_updates(config, current_list, updatestack).await?; Ok(()) } -async fn specific_update(config: Cfg, list: List, project: Project) -> Result> { +async fn specific_update(config: Cfg, input: Input, list: List, project: Project) -> Result> { print!("Checking update for '{}' in {}", project.title, list.id); let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; @@ -54,7 +65,7 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result = vec![]; - if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? { + if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { //get new versions print!(" | getting new version"); let current_str = extract_current_version(applicable_versions.clone())?; @@ -71,9 +82,9 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result) -> Result> { +async fn download_updates(config: Cfg, current_list: List, versions: Vec) -> Result> { - let dl_path = String::from(&config.downloads); + let dl_path = String::from(¤t_list.download_folder); for ver in versions { let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); -- cgit v1.2.3