From 477e0ecbb7bb34b581c518bfc2bc7ebc210b4673 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sat, 19 Nov 2022 21:31:37 +0100 Subject: added clean to download --- Cargo.lock | 2 +- Cargo.toml | 2 +- planmodlist.xopp | Bin 197609 -> 197651 bytes src/commands/download.rs | 19 ++++++++++++++----- src/input.rs | 4 ++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a0d2fd..29ff9de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -647,7 +647,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.2.1" +version = "0.2.2" dependencies = [ "chrono", "config", diff --git a/Cargo.toml b/Cargo.toml index 336c8e6..f0f8b55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.2.1" +version = "0.2.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 7dbf105..a1fd2fa 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/commands/download.rs b/src/commands/download.rs index 993294b..db0fc93 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -4,22 +4,31 @@ use reqwest::Client; use futures_util::StreamExt; -use crate::{get_current_list, config::Cfg, db::userlist_get_all_downloads}; +use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; -pub async fn download(config: Cfg) -> Result<(), Box> { +pub async fn download(config: Cfg, input: Input) -> Result<(), Box> { let list = get_current_list(config.clone())?; - let links = userlist_get_all_downloads(config.clone(), list.id)?; + let links = userlist_get_all_downloads(config.clone(), list.clone().id)?; - download_links(config, links).await?; + download_links(config, input, list, links).await?; Ok(()) } -async fn download_links(config: Cfg, links: Vec) -> Result> { +async fn download_links(config: Cfg, input: Input, current_list: List, links: Vec) -> Result> { let dl_path = String::from(&config.downloads); + 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())?; + } + } + for link in links { let filename = link.split('/').last().unwrap(); let dl_path_file = format!("{}/{}", config.downloads, filename); diff --git a/src/input.rs b/src/input.rs index 109fa0c..19aa2c2 100644 --- a/src/input.rs +++ b/src/input.rs @@ -122,7 +122,7 @@ pub async fn get_input(config: Cfg) -> Result<(), Box> { setup(config).await }, Cmd::Download => { - download(config).await + download(config, input).await } } } @@ -130,6 +130,6 @@ pub async fn get_input(config: Cfg) -> Result<(), Box> { #[test] fn input_from() { let string = "lis add test 1.19.2 fabric"; - let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), force_download: false, direct_download: false, all_lists: false }; + let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), force_download: false, direct_download: false, all_lists: false, clean: false, delete_old: false }; assert_eq!(Input::from(string).unwrap(), input); } -- cgit v1.2.3