use std::io::{self, Write as _};
use std::path::Path;
use anyhow::Result;
use openmoat_hosts::Host;
use crate::home::Home;
use crate::protection::Protection;
use crate::render::{Deferred, clean};
use crate::report::{self, Attention, Mark, Style};
#[derive(Debug, Default)]
pub(super) struct Steps {
pub trust: Vec<(&'static str, &'static str)>,
pub update: Option<String>,
pub changed: bool,
}
impl Steps {
fn lines(&self, protected: usize) -> Vec<String> {
let mut lines: Vec<String> = self
.trust
.iter()
.map(|(name, step)| format!("{name}: {step}"))
.collect();
if let Some(update) = &self.update {
lines.push(format!(
"Review the new default rules ({update}): openmoat policy upgrade"
));
}
if self.changed {
lines.push("Restart your agents (a new session reads the new settings)".to_owned());
}
if protected > 0 {
lines.push("Try it: openmoat policy check \"cat ~/.ssh/id_rsa\"".to_owned());
}
lines
}
}
pub(super) fn write(
home: &Home,
binary: &Path,
hosts: &[Host],
steps: &Steps,
backups: usize,
verbose: bool,
out: &mut Deferred,
) -> Result<()> {
let agents: Vec<_> = crate::protection::report(home, binary)?
.into_iter()
.filter(|a| hosts.iter().any(|h| h.id() == a.host))
.collect();
let done = Done {
agents: &agents,
steps,
backups,
verbose,
};
let mut text = Vec::new();
done.render(Style::detect(), &report::Paths::detect(), &mut text)?;
out.write_all(&text)?;
Ok(())
}
pub(super) struct Done<'a> {
pub agents: &'a [Protection],
pub steps: &'a Steps,
pub backups: usize,
pub verbose: bool,
}
impl Done<'_> {
pub fn render(&self, style: Style, paths: &report::Paths, out: &mut Vec<u8>) -> io::Result<()> {
let (agents, steps, verbose) = (self.agents, self.steps, self.verbose);
let protected = agents.iter().filter(|a| a.level.hooked()).count();
if !agents.is_empty() {
writeln!(out)?;
let (mark, line) = if protected == agents.len() {
(
Mark::Ok,
format!("{} protected", report::count(protected, "agent")),
)
} else {
let all = report::count(agents.len(), "agent");
(Mark::Attention, format!("{protected} of {all} protected"))
};
writeln!(out, "{} {line}", style.mark(mark))?;
let table = report::agent_table(agents, &Attention::default(), verbose);
write_table(&table, style, paths, out)?;
}
let lines = steps.lines(protected);
if !lines.is_empty() {
writeln!(out)?;
writeln!(out, "{}", style.bold("Next steps"))?;
for (n, line) in lines.iter().enumerate() {
writeln!(out, " {}. {}", n + 1, clean(line))?;
}
}
let mut footer = Vec::new();
if self.backups > 0 {
footer.push("Backups saved");
}
if !agents.is_empty() {
footer.push("undo everything: openmoat uninstall");
}
footer.push("check any time: openmoat status");
if !verbose {
footer.push("details: openmoat init --verbose");
}
writeln!(out)?;
writeln!(out, "{}", style.dim(&footer.join(" ยท ")))
}
}
fn write_table(
table: &report::AgentTable,
style: Style,
paths: &report::Paths,
out: &mut Vec<u8>,
) -> io::Result<()> {
let width = table
.rows
.iter()
.map(|r| r.label.chars().count())
.max()
.unwrap_or(0)
.max("Agent".len());
writeln!(out)?;
let header = format!(" {:<width$} Protection", "Agent");
writeln!(out, "{}", style.dim(&header))?;
for row in &table.rows {
let text = paths.shorten(&clean(&row.text));
let text = text.trim_end();
writeln!(
out,
" {:<width$} {} {text}",
clean(&row.label),
style.mark(row.mark)
)?;
}
Ok(())
}
#[cfg(test)]
mod tests;