use std::io::Write;
use std::path::Path;
use crate::analysis::AnalysisResult;
use crate::cli::cli_error::CliError;
use crate::cli::cli_error::CliResult;
use crate::config::Config;
use crate::suppression::baseline_file::BaselineFile;
use crate::suppression::baseline_file::baseline_path;
pub struct BaselineCommand<'a> {
root: &'a Path,
config: &'a Config,
result: &'a AnalysisResult,
dry_run: bool,
}
impl<'a> BaselineCommand<'a> {
#[must_use]
pub const fn new(
root: &'a Path,
config: &'a Config,
result: &'a AnalysisResult,
dry_run: bool,
) -> Self {
Self {
root,
config,
result,
dry_run,
}
}
pub fn run(&self, writer: &mut impl Write) -> CliResult {
let path = baseline_path(self.root, self.config.baseline.as_deref());
let recorded = BaselineFile::record(self.result);
for entry in &recorded.entries {
writeln!(
writer,
" {} {} ({} members: {})",
entry.kind,
entry.fingerprint,
entry.members,
entry.names.join(", ")
)?;
}
if self.dry_run {
writeln!(
writer,
"{} groups would be recorded in {}.",
recorded.len(),
path.display()
)?;
return Ok(());
}
recorded.save(&path).map_err(CliError::Analysis)?;
writeln!(
writer,
"Recorded {} groups in {}.",
recorded.len(),
path.display()
)?;
Ok(())
}
}