From 93e61a4bd6ad8b5db1083bdd21994bf73b0b90ba Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 17 Apr 2023 20:30:16 +0200 Subject: added clap cli, modified (basically) all user interface functions; changed some functions to easier string handling --- src/commands/download.rs | 12 +++++----- src/commands/io.rs | 29 ++++++------------------ src/commands/list.rs | 53 +++++++++++++------------------------------- src/commands/modification.rs | 44 ++++++++++++------------------------ src/commands/update.rs | 22 ++++-------------- 5 files changed, 46 insertions(+), 114 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 2714630..4baecee 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,10 +1,10 @@ use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version, clean_list_dir}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}}; -use crate::{List, get_current_list, config::Cfg, input::Input}; +use crate::{List, get_current_list, config::Cfg}; -pub async fn download(config: Cfg, input: Input) -> MLE<()> { +pub async fn download(config: Cfg, all_lists: bool, clean: bool, delete_old: bool) -> MLE<()> { let mut liststack: Vec = vec![]; - if input.all_lists { + if all_lists { let list_ids = lists_get_all_ids(config.clone())?; for id in list_ids { liststack.push(lists_get(config.clone(), id)?); @@ -33,7 +33,7 @@ pub async fn download(config: Cfg, input: Input) -> MLE<()> { let current_download = downloaded_versions.get(&mod_id); - if current_download.is_none() || input.clean { + if current_download.is_none() || clean { to_download.push(current_version); } else { let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG").unwrap(); @@ -44,7 +44,7 @@ pub async fn download(config: Cfg, input: Input) -> MLE<()> { } } - if input.clean { clean_list_dir(¤t_list)? }; + if clean { clean_list_dir(¤t_list)? }; if !to_download.is_empty() { download_versions(current_list.clone(), config.clone(), get_raw_versions(&config.apis.modrinth, to_download).await).await?; @@ -54,7 +54,7 @@ pub async fn download(config: Cfg, input: Input) -> MLE<()> { if !to_disable.is_empty() { for ver in to_disable { - if input.delete_old { + if delete_old { println!("Deleting version {} for mod {}", ver.1, ver.0); delete_version(current_list.clone(), ver.1)?; } else { diff --git a/src/commands/io.rs b/src/commands/io.rs index a3d056f..5de8dd1 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::prelude::*; use serde::{Serialize, Deserialize}; -use crate::{input::{Input, IoOptions}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, List, devdir, error::MLE, mods_add, IDSelector}; +use crate::{db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, List, devdir, error::MLE, mod_add, IDSelector}; #[derive(Debug, Serialize, Deserialize)] struct Export { @@ -32,22 +32,12 @@ impl ExportList { } } -pub async fn io(config: Cfg, input: Input) -> MLE<()> { - - match input.clone().io_options.unwrap() { - IoOptions::Export => { export(config, input)? }, - IoOptions::Import => { import(config, input).await? }, - } - - Ok(()) -} - -fn export(config: Cfg, input: Input) -> MLE<()> { +pub fn export(config: Cfg, list: Option) -> MLE<()> { let mut list_ids: Vec = vec![]; - if input.all_lists { + if list.is_none() { list_ids = lists_get_all_ids(config.clone())?; } else { - list_ids.push(lists_get(config.clone(), input.list.unwrap().id)?.id); + list_ids.push(lists_get(config.clone(), list.unwrap())?.id); } let mut lists: Vec = vec![]; for list_id in list_ids { @@ -64,14 +54,9 @@ fn export(config: Cfg, input: Input) -> MLE<()> { Ok(()) } -async fn import(config: Cfg, input: Input) -> MLE<()> { +pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> { - let filestr: String = match input.file { - Some(args) => args, - None => devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str()), - }; - - let mut file = File::open(filestr)?; + let mut file = File::open(file_str)?; let mut content = String::new(); file.read_to_string(&mut content)?; let export: Export = toml::from_str(&content)?; @@ -86,7 +71,7 @@ async fn import(config: Cfg, input: Input) -> MLE<()> { }; //TODO impl set_version and good direct download //TODO impl all at once, dafuck - mods_add(config.clone(), mod_ids, list, input.direct_download, false).await?; + mod_add(config.clone(), mod_ids, list, direct_download, false).await?; } Ok(()) } diff --git a/src/commands/list.rs b/src/commands/list.rs index 8e86973..80e801a 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,4 +1,4 @@ -use 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}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, update, error::MLE}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -8,44 +8,23 @@ pub struct List { pub download_folder: String, } -pub async fn list(config: Cfg, input: Input) -> MLE<()> { - - match input.clone().list_options.unwrap() { - ListOptions::Add => { - add(config, input) - }, - ListOptions::Change => { - change(config, input) - }, - ListOptions::Remove => { - remove(config, input) - }, - ListOptions::Version => { - version(config, input).await - } - } -} - pub fn get_current_list(config: Cfg) -> MLE { let id = config_get_current_list(config.clone())?; lists_get(config, id) } -fn add(config: Cfg, input: Input) -> MLE<()> { - let id = input.list_id.unwrap(); - let mc_version = input.list_mcversion.unwrap(); - let mod_loader = input.modloader.unwrap(); - let download_folder = input.directory.unwrap(); - lists_insert(config, id, mc_version, mod_loader, download_folder) +pub fn list_add(config: Cfg, id: String, mc_version: String, modloader: Modloader, directory: String) -> MLE<()> { + lists_insert(config, id, mc_version, modloader, directory) } -fn change(config: Cfg, input: Input) -> MLE<()> { - println!("Change default list to: {}", input.clone().list.unwrap().id); - config_change_current_list(config, input.list.unwrap().id) +pub fn list_change(config: Cfg, id: String) -> MLE<()> { + //TODO check if list exists + println!("Change default list to: {}", id); + config_change_current_list(config, id) } -fn remove(config: Cfg, input: Input) -> MLE<()> { - lists_remove(config, input.list.unwrap().id) +pub fn list_remove(config: Cfg, id: String) -> MLE<()> { + lists_remove(config, id) } ///Changing the current lists version and updating it @@ -54,14 +33,12 @@ fn remove(config: Cfg, input: Input) -> MLE<()> { /// /// * `config` - The current config /// * `args` - All args, to extract the new version -async fn version(config: Cfg, input: Input) -> MLE<()> { - println!("Change version for list {} to minecraft version: {}", input.clone().list.unwrap().id, input.clone().list_mcversion.unwrap()); +pub async fn list_version(config: Cfg, id: String, mc_version: String, download: bool, delete: bool) -> MLE<()> { + println!("Change version for list {} to minecraft version: {}", id, mc_version); - lists_version(config.clone(), input.clone().list.ok_or("").unwrap().id, input.clone().list_mcversion.ok_or("").unwrap())?; - - //Linebreak readability - println!(""); + lists_version(config.clone(), &id, &mc_version)?; - println!("Check for updates for new minecraft version in list {}", input.clone().list.unwrap().id); - cmd_update(config, vec![input.list.ok_or("").unwrap()], true, input.direct_download, input.delete_old).await + println!("\nCheck for updates for new minecraft version in list {}", id); + let list = lists_get(config.clone(), id)?; + update(config, vec![list], true, download, delete).await } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 31e50af..454e148 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,4 +1,4 @@ -use crate::{modrinth::{versions, extract_current_version, Version, projects, get_raw_versions, project}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, 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::{versions, extract_current_version, Version, projects, get_raw_versions, project}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum IDSelector { @@ -6,24 +6,6 @@ pub enum IDSelector { VersionID(String) } -pub async fn modification(config: Cfg, input: Input) -> MLE<()> { - match input.clone().mod_options.ok_or("").unwrap() { - ModOptions::Add => { - add(config, input).await - }, - ModOptions::Remove => { - remove(config, input) - }, - } -} - -async fn add(config: Cfg, input: Input) -> MLE<()> { - - mods_add(config, vec![input.mod_id.unwrap()], input.list.unwrap(), input.direct_download, input.set_version).await?; - - Ok(()) -} - #[derive(Debug, Clone)] pub struct ProjectInfo { pub mod_id: String, @@ -34,7 +16,7 @@ pub struct ProjectInfo { pub download_link: String, } -pub async fn mods_add(config: Cfg, ids: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { +pub async fn mod_add(config: Cfg, ids: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { println!("Add mods to {}", list.id); println!(" └Add mods:"); @@ -156,22 +138,24 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE Ok(projectinfo) } -fn remove(config: Cfg, input: Input) -> MLE<()> { - - let id = match input.clone().mod_id.unwrap() { - IDSelector::ModificationID(id) => id, - IDSelector::VersionID(..) => return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ID")), - }; +/// Remove mod from a list +/// # Arguments +/// +/// * `config` - config struct +/// * `id` - name, slug or id of the mod +/// * `list` - List struct +pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { - let mod_id = mods_get_id(&config.data, &id)?; + let mod_id = mods_get_id(&config.data, id)?; - let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; + let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; - userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; - delete_version(input.list.unwrap(), version)?; + userlist_remove(config.clone(), &list.id, &mod_id)?; + delete_version(list, version)?; let list_ids = lists_get_all_ids(config.clone())?; + // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { let mods = userlist_get_all_ids(config.clone(), id)?; diff --git a/src/commands/update.rs b/src/commands/update.rs index 75bee39..e5751c0 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,21 +1,6 @@ -use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, userlist_get_set_version, mods_get_info}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; - -pub async fn update(config: Cfg, input: Input) -> MLE<()> { - let mut liststack: Vec = vec![]; - if input.all_lists { - let list_ids = lists_get_all_ids(config.clone())?; - for id in list_ids { - liststack.push(lists_get(config.clone(), id)?); - } - } else { - let current = get_current_list(config.clone())?; - println!("Update list {}:", current.id); - liststack.push(current) - } - cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await -} +use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, userlist_get_current_version, userlist_get_set_version, mods_get_info}, List, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; -pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { +pub async fn update(config: Cfg, liststack: Vec, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { for current_list in liststack { let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; @@ -34,7 +19,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d } //Getting current installed version for disable or delete - let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&id))?; + let disable_version = userlist_get_current_version(config.clone(), ¤t_list.id, &id)?; updatestack.push( match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await { @@ -110,6 +95,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML }?; current.push(current_ver.clone()); + //TODO implement version selection if no primary let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { Ok(p) => Ok(p), Err(e) => Err(MLError::new(ErrorType::Other, e)), -- cgit v1.2.3