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 --- src/commands/download.rs | 19 ++++++++++++++----- src/input.rs | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') 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