use super::read::{combine_path_like, read_path};
use super::write::replace_path;
use clap::{crate_authors, Parser};
use std::io;
use std::path::PathBuf;
#[derive(Debug, Parser)]
#[clap(
about = "Remove a directory",
author = crate_authors!(),
visible_alias = "del",
)]
pub struct RmOpt {
#[clap(default_value = ".")]
dir: PathBuf,
#[clap(short, long)]
quiet: bool,
#[clap(short = 'H', long)]
history: bool,
#[clap(short = 'n', long = "dry-run")]
dry_run: bool,
}
impl RmOpt {
pub fn validate(&self) -> io::Result<()> {
if !self.dir.exists() {
let err_nonexistent = io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Directory `{}` does not exist. `Please double check the directory you intend to remove.",
self.dir.display()
)
);
if !self.quiet {
eprintln!("{}", err_nonexistent);
}
return Err(err_nonexistent);
}
let current_path = read_path();
if !current_path.iter().any(|x| *x == self.dir) {
let err_not_found = io::Error::new(
io::ErrorKind::NotFound,
format!(
"Directory `{}` not found in `$PATH`. No changes made.",
self.dir.display()
),
);
if !self.quiet {
eprintln!("{}", err_not_found);
}
return Err(err_not_found);
}
Ok(())
}
}
pub fn rm_from_path(opts: &RmOpt) -> io::Result<()> {
let current_path = read_path();
let i = current_path.iter().position(|x| *x == opts.dir).unwrap();
let mut vpath = current_path;
vpath.remove(i);
let newpath = combine_path_like(vpath)?;
match replace_path(newpath, opts.dry_run, opts.history, opts.quiet) {
Ok(()) => Ok(()),
Err(e) => {
if !opts.quiet {
eprintln!("{}", e);
}
Err(e)
}
}
}