feat: error handling if removing file fails
To think this got left out for this long...
This commit is contained in:
parent
032f3a2c75
commit
6516e2f2b8
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -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",
|
||||
]
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
28
src/main.rs
28
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<String>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue