From c966bc6827cb2a9fb8800262911caeb1b9715809 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Tue, 26 Mar 2024 13:54:15 +0000 Subject: [PATCH] feat: error handling if removing file fails To think this got left out for this long... --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 28 +++++++++++++++++++--------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d128c66..3a5a308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,12 @@ dependencies = [ "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]] name = "clap" version = "4.5.2" @@ -169,6 +175,7 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" name = "vaporise" version = "0.2.1" dependencies = [ + "anyhow", "clap", "colored", ] diff --git a/Cargo.toml b/Cargo.toml index 0589443..bf7b61e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ codegen-units = 1 panic = "abort" [dependencies] +anyhow = "1.0.81" clap = { version = "4.5.2", features = ["derive"] } colored = "2.1.0" diff --git a/src/main.rs b/src/main.rs index 58e90e1..57c0456 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use colored::Colorize; use std::{fs, path, process}; +use anyhow::{Context, Result}; #[derive(Parser, Debug)] #[command(version, about, long_about = None, author)] @@ -14,34 +15,37 @@ struct Args { targets: Vec, } -fn main() -> std::io::Result<()> { +fn vaporise() -> Result<()> { let args = Args::parse(); if args.targets.is_empty() { - println!("{} no arguments passed", "error:".red().bold()); + println!("{}: no arguments passed", "error".red().bold()); println!( - "{} try 'vpr -h' for more information", - "note:".cyan().bold() + "{}: try 'vpr -h' for more information", + "note".cyan().bold() ); process::exit(0); } + for target in args.targets.iter() { 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); } if path::Path::new(target).exists() { 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 { - let _ = fs::remove_file(target); + let _ = fs::remove_file(target) + .with_context(|| format!("could not remove file: {}", target.bold()))?; } } else { println!( - "{} the specified target does not exist {}", - "error:".red().bold(), + "{}: the specified target does not exist {}", + "error".red().bold(), target.yellow() ); } @@ -49,3 +53,9 @@ fn main() -> std::io::Result<()> { Ok(()) } + +fn main() { + if let Err(error) = vaporise() { + println!("{}: {:?}", "error".red().bold(), error); + } +}