Skip to content

Commit

Permalink
Support deleting multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
gcapizzi committed Apr 3, 2024
1 parent 31830d6 commit 3a9fd77
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
25 changes: 16 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &'a Utf8Path>,
{
let filenames = paths
.into_iter()
.map(|p| ("filenames[]", p.as_str()))
.collect::<Vec<_>>();
ureq::post("https://neocities.org/api/delete")
.set("Authorization", &format!("Bearer {}", self.api_key))
.send_form(&[("filenames[]", f.as_str())])?;
.send_form(&filenames)?;

Ok(())
}
Expand All @@ -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(())
}
Expand Down
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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::<Utf8PathBuf>("path").unwrap()),
Some(("delete", m)) => delete(&m.get_one::<Utf8PathBuf>("path").unwrap()),
Some(("delete", m)) => delete(m.get_many::<Utf8PathBuf>("path").unwrap()),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -52,7 +60,7 @@ fn push<'a, I: IntoIterator<Item = &'a Utf8PathBuf>>(paths: I) -> Result<()> {
Ok(())
}

fn delete(f: &Utf8Path) -> Result<()> {
client()?.delete(f)?;
fn delete<'a, I: IntoIterator<Item = &'a Utf8PathBuf>>(paths: I) -> Result<()> {
client()?.delete(paths.into_iter().map(|p| p.as_path()))?;
Ok(())
}
14 changes: 3 additions & 11 deletions tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -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(())
}
Expand Down

0 comments on commit 3a9fd77

Please sign in to comment.