use super::{history::get_nth_last_revision, write::replace_path};
use clap::{crate_authors, Parser};
use std::io;
#[derive(Debug, Parser)]
#[clap(
about = "Revert to a previous version of `$PATH`",
author = crate_authors!(),
)]
pub struct RevertOpt {
#[clap(default_value = "1")]
revision: u128,
#[clap(short, long)]
quiet: bool,
#[clap(short = 'H', long)]
history: bool,
#[clap(short = 'n', long = "dry-run")]
dry_run: bool,
}
impl RevertOpt {
pub fn validate(&self) -> io::Result<()> {
Ok(())
}
}
pub fn revert_path(opts: &RevertOpt) -> io::Result<()> {
let newpath = match get_nth_last_revision(opts.revision) {
Ok(s) => s,
Err(e) => {
if !opts.quiet {
eprintln!("{}", e);
}
return Err(e);
}
};
match replace_path(newpath, opts.dry_run, opts.history, opts.quiet) {
Ok(()) => Ok(()),
Err(e) => {
if !opts.quiet {
eprintln!("Could not revert `$PATH`. {}. No changes made.", e);
}
Err(e)
}
}
}