Skip to content

Commit

Permalink
Simplify remove_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Apr 8, 2024
1 parent 6a8e2b1 commit 7fc772f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ where
}
Ok(())
}

pub fn db_remove(db: Option<&sled::Db>, key: &str) -> anyhow::Result<()> {
if let Some(db) = db {
db.remove(key)?;
}
Ok(())
}
35 changes: 14 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;
use std::{collections::HashMap, io::Write, net::IpAddr, path::PathBuf};
use tracing_subscriber::EnvFilter;
use url::Url;
use yukina::{db_set, RemoteSizeDBItem};
use yukina::{db_remove, db_set, RemoteSizeDBItem};

use shadow_rs::shadow;
shadow!(build);
Expand Down Expand Up @@ -251,28 +251,21 @@ where

fn remove_file(args: &Cli, path: &str, local_db: Option<&sled::Db>) -> Result<(), std::io::Error> {
let full_path = args.repo_path.join(path);
if full_path.exists() {
if args.dry_run {
tracing::info!("Would remove: {:?}", full_path);
} else if let Err(e) = std::fs::remove_file(&full_path) {
tracing::warn!("Remove file failed: {:?}", e);
return Err(e);
} else {
tracing::info!("Removed: {:?}", full_path);
if let Some(db) = local_db {
if let Err(e) = db.remove(path) {
tracing::warn!("Remove from local db failed: {:?}", e);
}
}
}
if args.dry_run {
tracing::info!("Would remove: {:?}", full_path);
return Ok(());
}
if let Err(e) = std::fs::remove_file(&full_path) {
tracing::warn!("Remove file failed: {:?}", e);
return Err(e);
}

Ok(())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File not found: {:?}", full_path),
))
tracing::info!("Removed: {:?}", full_path);
if let Err(e) = db_remove(local_db, path) {
tracing::warn!("Remove from local db failed: {:?}", e);
}

Ok(())
}

async fn head_file(args: &Cli, url: &str, client: &reqwest::Client) -> Result<reqwest::Response> {
Expand Down

0 comments on commit 7fc772f

Please sign in to comment.