feat: error handling if removing file fails

To think this got left out for this long...
This commit is contained in:
Muhammad Nauman Raza 2024-03-26 13:54:15 +00:00
parent 032f3a2c75
commit 6516e2f2b8
Signed by: devraza
GPG key ID: 91EAD6081011574B
3 changed files with 27 additions and 9 deletions

7
Cargo.lock generated
View file

@ -50,6 +50,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "anyhow"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.2" version = "4.5.2"
@ -169,6 +175,7 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
name = "vaporise" name = "vaporise"
version = "0.2.1" version = "0.2.1"
dependencies = [ dependencies = [
"anyhow",
"clap", "clap",
"colored", "colored",
] ]

View file

@ -15,6 +15,7 @@ codegen-units = 1
panic = "abort" panic = "abort"
[dependencies] [dependencies]
anyhow = "1.0.81"
clap = { version = "4.5.2", features = ["derive"] } clap = { version = "4.5.2", features = ["derive"] }
colored = "2.1.0" colored = "2.1.0"

View file

@ -1,6 +1,7 @@
use clap::Parser; use clap::Parser;
use colored::Colorize; use colored::Colorize;
use std::{fs, path, process}; use std::{fs, path, process};
use anyhow::{Context, Result};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None, author)] #[command(version, about, long_about = None, author)]
@ -14,34 +15,37 @@ struct Args {
targets: Vec<String>, targets: Vec<String>,
} }
fn main() -> std::io::Result<()> { fn vaporise() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
if args.targets.is_empty() { if args.targets.is_empty() {
println!("{} no arguments passed", "error:".red().bold()); println!("{}: no arguments passed", "error".red().bold());
println!( println!(
"{} try 'vpr -h' for more information", "{}: try 'vpr -h' for more information",
"note:".cyan().bold() "note".cyan().bold()
); );
process::exit(0); process::exit(0);
} }
for target in args.targets.iter() { for target in args.targets.iter() {
if !args.no_preserve && (target == "/" || target == "~") { if !args.no_preserve && (target == "/" || target == "~") {
println!("{} you're trying to delete an important directory ({})! specify '{}' if you really want to do this", "error:".red().bold(), "--no-preserve".yellow(), target); println!("{}: you're trying to delete an important directory ({})! specify '{}' if you really want to do this", "error".red().bold(), "--no-preserve".yellow(), target);
process::exit(0); process::exit(0);
} }
if path::Path::new(target).exists() { if path::Path::new(target).exists() {
if fs::metadata(target).unwrap().is_dir() { if fs::metadata(target).unwrap().is_dir() {
let _ = fs::remove_dir_all(target); let _ = fs::remove_dir_all(target)
.with_context(|| format!("could not remove directory: {}", target.bold()))?;
} else { } else {
let _ = fs::remove_file(target); let _ = fs::remove_file(target)
.with_context(|| format!("could not remove file: {}", target.bold()))?;
} }
} else { } else {
println!( println!(
"{} the specified target does not exist {}", "{}: the specified target does not exist {}",
"error:".red().bold(), "error".red().bold(),
target.yellow() target.yellow()
); );
} }
@ -49,3 +53,9 @@ fn main() -> std::io::Result<()> {
Ok(()) Ok(())
} }
fn main() {
if let Err(error) = vaporise() {
println!("{}: {:?}", "error".red().bold(), error);
}
}