From 28706f6edf10a135a67334d7035948bab4064bef Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 18 Dec 2022 23:11:50 +0100 Subject: dl add clean & all-lists; start of io --- Cargo.lock | 2 +- Cargo.toml | 2 +- planmodlist.xopp | Bin 244591 -> 244518 bytes src/commands/download.rs | 90 +++++++++++++++++++++++++++++------------------ src/commands/io.rs | 12 +++++++ src/commands/list.rs | 3 ++ src/commands/mod.rs | 2 ++ src/commands/update.rs | 2 +- src/input.rs | 15 ++++++-- 9 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 src/commands/io.rs diff --git a/Cargo.lock b/Cargo.lock index 3abb880..6c10e82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -647,7 +647,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.5.1" +version = "0.5.2" dependencies = [ "chrono", "config", diff --git a/Cargo.toml b/Cargo.toml index b8091cd..ccbc57e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.5.1" +version = "0.5.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/planmodlist.xopp b/planmodlist.xopp index bf0c5f2..6405294 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/commands/download.rs b/src/commands/download.rs index 9f70499..0f9011c 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,49 +1,69 @@ -use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::userlist_get_all_current_versions_with_mods, modrinth::get_raw_versions}; -#[allow(unused_imports)] -use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; +use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions}; +use crate::{List, get_current_list, config::Cfg, input::Input}; pub async fn download(config: Cfg, input: Input) -> Result<(), Box> { - let current_list = get_current_list(config.clone())?; + 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!("Checking for updates of mods in {}", current.id); + liststack.push(current) + } - let downloaded_versions = get_downloaded_versions(current_list.clone())?; - let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; + for current_list in liststack { + let downloaded_versions = get_downloaded_versions(current_list.clone())?; + let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; - let mut to_download: Vec = vec![]; - //(mod_id, version_id) - let mut to_disable: Vec<(String, String)> = vec![]; + let mut to_download: Vec = vec![]; + //(mod_id, version_id) + let mut to_disable: Vec<(String, String)> = vec![]; - for version in current_version_ids { - let mod_id = version.0; - let current_version = version.1; + for version in current_version_ids { + let mod_id = version.0; + let current_version = version.1; - let current_download = downloaded_versions.get(&mod_id); + let current_download = downloaded_versions.get(&mod_id); - if current_download.is_none() { - to_download.push(current_version); - } else { - let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; - if ¤t_version != downloaded_version { - to_disable.push((mod_id.clone(), String::from(downloaded_version))); + if current_download.is_none() || input.clean { to_download.push(current_version); + } else { + let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; + if ¤t_version != downloaded_version { + to_disable.push((mod_id.clone(), String::from(downloaded_version))); + to_download.push(current_version); + } } } - } - - if !to_download.is_empty() { - download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; - } else { - println!("There are no new versions to download"); - } - - if !to_disable.is_empty() { - for ver in to_disable { - if input.delete_old { - println!("Deleting version {} for mod {}", ver.1, ver.0); - delete_version(current_list.clone(), ver.1)?; - } else { - disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; - }; + + 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())?; + } + } + + if !to_download.is_empty() { + download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; + } else { + println!("There are no new versions to download"); + } + + if !to_disable.is_empty() { + for ver in to_disable { + if input.delete_old { + println!("Deleting version {} for mod {}", ver.1, ver.0); + delete_version(current_list.clone(), ver.1)?; + } else { + disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; + }; + } } } diff --git a/src/commands/io.rs b/src/commands/io.rs new file mode 100644 index 0000000..dc1f408 --- /dev/null +++ b/src/commands/io.rs @@ -0,0 +1,12 @@ +use crate::{input::{Input, Subcmd}, config::Cfg}; + +pub fn io(_config: Cfg, input: Input) -> Result<(), Box> { + + match input.subcommand.ok_or("INVALID_INPUT")? { + Subcmd::Export => {}, + Subcmd::Import => {}, + _ => {}, + } + + Ok(()) +} diff --git a/src/commands/list.rs b/src/commands/list.rs index 76965df..096ce65 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -21,6 +21,9 @@ pub fn list(config: Cfg, input: Input) -> Result<(), Box> }, Subcmd::Remove => { remove(config, input.args.ok_or("")?) + }, + _ => { + Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) } } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 20badcb..0d5bd00 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,9 +3,11 @@ pub mod list; pub mod update; pub mod setup; pub mod download; +pub mod io; pub use modification::*; pub use list::*; pub use update::*; pub use setup::*; pub use download::*; +pub use io::*; diff --git a/src/commands/update.rs b/src/commands/update.rs index 498b6a9..0895efb 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -41,7 +41,7 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box { current_versions.push((disable_version, p_id)); diff --git a/src/input.rs b/src/input.rs index 41b0c29..ffc1213 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,5 @@ use std::env; -use crate::{config::Cfg, list, modification, update, setup, download, error::{MLError, ErrorType, MLE}}; +use crate::{config::Cfg, list, modification, update, setup, download, io, error::{MLError, ErrorType, MLE}}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Input { @@ -77,7 +77,8 @@ pub enum Cmd { List, Update, Download, - Setup + Setup, + Io } impl Cmd { @@ -88,6 +89,7 @@ impl Cmd { "update" => Self::Update, "download" => Self::Download, "setup" => Self::Setup, + "io" => Self::Io, _ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command")) }; Ok(cmd) @@ -98,7 +100,9 @@ impl Cmd { pub enum Subcmd { Add, Remove, - Change + Change, + Export, + Import, } impl Subcmd { @@ -107,6 +111,8 @@ impl Subcmd { "add" => Self::Add, "remove" => Self::Remove, "change" => Self::Change, + "export" => Self::Export, + "import" => Self::Import, _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND")) }; Ok(cmd) @@ -136,6 +142,9 @@ pub async fn get_input(config: Cfg) -> Result<(), Box> { }, Cmd::Download => { download(config, input).await + }, + Cmd::Io => { + io(config, input) } } } -- cgit v1.2.3