2024-03-13 19:27:25 +00:00
use std ::{ fs , path , process } ;
use clap ::Parser ;
use colored ::Colorize ;
#[ derive(Parser, Debug) ]
2024-03-13 20:23:48 +00:00
#[ command(version, about, long_about = None, author) ]
2024-03-13 20:20:31 +00:00
/// A simple, featureful and blazingly fast memory-safe alternative to 'rm' written in Rust.
2024-03-13 19:27:25 +00:00
struct Args {
2024-03-13 20:20:31 +00:00
/// Recursively delete files and directories
2024-03-13 19:27:25 +00:00
#[ arg(short, long) ]
recursive : bool ,
2024-03-13 20:20:31 +00:00
/// Don't try to preserve '~' or '/'
2024-03-13 20:03:48 +00:00
#[ arg(long) ]
2024-03-13 20:05:55 +00:00
no_preserve : bool ,
2024-03-13 20:03:48 +00:00
2024-03-13 19:27:25 +00:00
#[ arg(trailing_var_arg = true, allow_hyphen_values = false) ]
targets : Vec < String > ,
}
fn main ( ) -> std ::io ::Result < ( ) > {
let args = Args ::parse ( ) ;
if args . targets . len ( ) = = 0 {
println! ( " {} {} " , " error: " . red ( ) . bold ( ) , " no arguents passed " ) ;
println! ( " {} {} " , " note: " . cyan ( ) . bold ( ) , " try 'vpr -h' for more information " ) ;
process ::exit ( 0 ) ;
}
2024-03-13 20:03:48 +00:00
2024-03-13 19:27:25 +00:00
for target in args . targets . iter ( ) {
2024-03-13 20:05:55 +00:00
if args . no_preserve = = false {
2024-03-13 20:03:48 +00:00
if target = = " / " | | target = = " ~ " {
println! ( " {} you're trying to delete an important directory ( {} )! specify '--no-preserve-root' if you really want to do this " , " error: " . red ( ) . bold ( ) , target ) ;
process ::exit ( 0 ) ;
}
}
2024-03-13 19:27:25 +00:00
if path ::Path ::new ( target ) . exists ( ) {
if fs ::metadata ( target ) . unwrap ( ) . is_dir ( ) {
if args . recursive = = false {
println! ( " {} could not remove directory {} , please specify '-r' " , " error: " . red ( ) . bold ( ) , target ) ;
process ::exit ( 0 ) ;
} else {
let _ = fs ::remove_dir_all ( target ) ;
}
} else {
let _ = fs ::remove_file ( target ) ;
}
} else {
println! ( " {} {} {} " , " error: " . red ( ) . bold ( ) , " the specified target does not exist: " , target ) ;
}
}
Ok ( ( ) )
}