From 3a9fd77b50a4f16cdb22110dba5c9d85ff2f4c48 Mon Sep 17 00:00:00 2001 From: Giuseppe Capizzi Date: Wed, 3 Apr 2024 16:14:26 +0200 Subject: [PATCH] Support deleting multiple files --- src/lib.rs | 25 ++++++++++++++++--------- src/main.rs | 20 ++++++++++++++------ tests/acceptance.rs | 14 +++----------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 44fb327..96356ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,10 +85,17 @@ impl Client { Ok(()) } - pub fn delete(&self, f: &Utf8Path) -> Result<()> { + pub fn delete<'a, I>(&self, paths: I) -> Result<()> + where + I: IntoIterator, + { + let filenames = paths + .into_iter() + .map(|p| ("filenames[]", p.as_str())) + .collect::>(); ureq::post("https://neocities.org/api/delete") .set("Authorization", &format!("Bearer {}", self.api_key)) - .send_form(&[("filenames[]", f.as_str())])?; + .send_form(&filenames)?; Ok(()) } @@ -109,22 +116,22 @@ mod tests { let (file3, file3_sha) = random_txt("file3.txt")?; client.push(vec![ - ("up/file1.txt".into(), file1.path().try_into()?), - ("up/file2.txt".into(), file2.path().try_into()?), - ("up/file3.txt".into(), file3.path().try_into()?), + ("up1/file.txt".into(), file1.path().try_into()?), + ("up2/file.txt".into(), file2.path().try_into()?), + ("up3/file.txt".into(), file3.path().try_into()?), ])?; let files = client.list()?; - let found_file1 = files.iter().find(|f| f.path == "up/file1.txt").unwrap(); - let found_file2 = files.iter().find(|f| f.path == "up/file2.txt").unwrap(); - let found_file3 = files.iter().find(|f| f.path == "up/file3.txt").unwrap(); + let found_file1 = files.iter().find(|f| f.path == "up1/file.txt").unwrap(); + let found_file2 = files.iter().find(|f| f.path == "up2/file.txt").unwrap(); + let found_file3 = files.iter().find(|f| f.path == "up3/file.txt").unwrap(); assert_eq!(Some(file1_sha), found_file1.sha1_hash); assert_eq!(Some(file2_sha), found_file2.sha1_hash); assert_eq!(Some(file3_sha), found_file3.sha1_hash); - client.delete("up".into())?; + client.delete(vec!["up1".into(), "up2".into(), "up3".into()])?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 1f42621..ceea8e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Result}; use camino::{Utf8Path, Utf8PathBuf}; -use clap::{value_parser, Arg, Command, ArgAction}; +use clap::{value_parser, Arg, ArgAction, Command}; fn main() -> Result<()> { env_logger::init(); @@ -9,17 +9,25 @@ fn main() -> Result<()> { .version("0.1") .subcommand(Command::new("list")) .subcommand( - Command::new("push").arg(Arg::new("path").action(ArgAction::Append).value_parser(value_parser!(Utf8PathBuf))), + Command::new("push").arg( + Arg::new("path") + .action(ArgAction::Append) + .value_parser(value_parser!(Utf8PathBuf)), + ), ) .subcommand( - Command::new("delete").arg(Arg::new("path").value_parser(value_parser!(Utf8PathBuf))), + Command::new("delete").arg( + Arg::new("path") + .action(ArgAction::Append) + .value_parser(value_parser!(Utf8PathBuf)), + ), ) .get_matches(); match matches.subcommand() { Some(("list", _)) => list(), Some(("push", m)) => push(m.get_many::("path").unwrap()), - Some(("delete", m)) => delete(&m.get_one::("path").unwrap()), + Some(("delete", m)) => delete(m.get_many::("path").unwrap()), _ => unreachable!(), } } @@ -52,7 +60,7 @@ fn push<'a, I: IntoIterator>(paths: I) -> Result<()> { Ok(()) } -fn delete(f: &Utf8Path) -> Result<()> { - client()?.delete(f)?; +fn delete<'a, I: IntoIterator>(paths: I) -> Result<()> { + client()?.delete(paths.into_iter().map(|p| p.as_path()))?; Ok(()) } diff --git a/tests/acceptance.rs b/tests/acceptance.rs index c9ccacc..4de8fe6 100644 --- a/tests/acceptance.rs +++ b/tests/acceptance.rs @@ -29,17 +29,7 @@ fn it_can_upload_a_file_and_delete_it() -> Result<()> { Command::cargo_bin("neo")? .arg("delete") .arg("file1.txt") - .assert() - .try_success()?; - - Command::cargo_bin("neo")? - .arg("delete") .arg("file2.txt") - .assert() - .try_success()?; - - Command::cargo_bin("neo")? - .arg("delete") .arg("file3.txt") .assert() .try_success()?; @@ -48,7 +38,9 @@ fn it_can_upload_a_file_and_delete_it() -> Result<()> { .arg("list") .assert() .try_success()? - .try_stdout(predicate::str::contains("file.txt").not())?; + .try_stdout(predicate::str::contains("file1.txt").not())? + .try_stdout(predicate::str::contains("file2.txt").not())? + .try_stdout(predicate::str::contains("file3.txt").not())?; Ok(()) }