use std::path::Path;
use crate::cli::cli_error::CliError;
use crate::cli::cli_error::CliResult;
use crate::cli::command_outcome::CommandOutcome;
use crate::fingerprint::Fingerprint;
use crate::suppression::ignore_file::IgnoreFile;
pub struct IgnoreCommand<'a> {
root: &'a Path,
fingerprint: &'a str,
reason: Option<&'a str>,
}
impl<'a> IgnoreCommand<'a> {
#[must_use]
pub const fn new(root: &'a Path, fingerprint: &'a str, reason: Option<&'a str>) -> Self {
Self {
root,
fingerprint,
reason,
}
}
pub fn run(&self) -> CliResult<CommandOutcome> {
let fingerprint = Fingerprint::from_hex(self.fingerprint)
.ok_or_else(|| CliError::InvalidFingerprint(self.fingerprint.to_owned()))?;
IgnoreFile::load(self.root)
.with_ignored(&fingerprint, self.reason.map(ToOwned::to_owned), vec![])
.save(self.root)?;
Ok(CommandOutcome::succeeded(format!(
"Added {} to ignore list.\n",
self.fingerprint
)))
}
}