use std::path::{Path, PathBuf};
use anyhow::Result;
use openmoat_hosts::Host;
use super::human::Place;
use super::{Area, Report};
use crate::home::Home;
use crate::install::{HookState, HostConfig, Recorded, dir_variable, env_config_dir, stale_hint};
use crate::integrity::{HookPinGap, Lock};
use crate::report::{Mark, Row};
pub(super) fn hooks(report: &mut Report, home: &Home, binary: &Path) -> Result<Vec<PathBuf>> {
let recorded = Recorded::load(home)?;
let mut hook_files = Vec::new();
crate::install::host_version::prefetch_present(home);
for host in Host::ALL {
let config = HostConfig::for_host(home, host)?;
let name = host.display_name();
if config.host_present() {
for line in crate::install::host_version::advisories(host) {
report.warn(&line);
}
}
for (fine, line) in crate::install::host_version::bundled_lines(host) {
let line = format!("{name:<16} {line}");
if fine {
report.note(&line);
} else {
report.warn(&line);
}
}
location(report, host, &config, &recorded)?;
let state = config.state(binary);
if config.host_present()
&& let Some(why) = crate::install::host_version::unsupported(host)
{
report.line(
Area::Hook,
false,
format!("{name:<16} not protected: {why}"),
);
continue;
}
if let Some(record) = crate::plugin_record::last(home, host) {
let text = format!("{name:<16} {}", crate::plugin_record::line(&record));
if matches!(state, HookState::Installed) {
report.note(&text);
} else {
report.line(Area::Hook, true, text);
if matches!(state, HookState::Missing) {
continue;
}
}
}
if recorded.skipped(host) && config.host_present() && matches!(state, HookState::Missing) {
report.note(&format!(
"{name:<16} not set up; `openmoat init --hosts {}` adds it",
host.id()
));
continue;
}
match state {
HookState::Installed => installed(report, home, &config, &mut hook_files),
HookState::Missing if !config.host_present() => {
report.note(&format!("{name:<16} host not found"));
}
HookState::Missing => {
report.line(
Area::Hook,
false,
format!("{name:<16} hook missing; run `openmoat init`"),
);
}
HookState::Stale { command } => {
report.line(
Area::Hook,
false,
format!("{name:<16} {}", stale_hint(&command)),
);
}
HookState::Unreadable(e)
| HookState::Unapproved(e)
| HookState::FailsOpen(e)
| HookState::TurnedOff(e) => {
report.line(Area::Hook, false, format!("{name:<16} {e}"));
}
}
}
for (host, found) in crate::install::detected()? {
let name = host.display_name();
report.note(&format!(
"{name:<16} {} {}",
host.spec().gaps,
found.display()
));
let text = format!("found at {}; {}", found.display(), host.spec().gaps);
report.human(Place::Agents, Row::new(Mark::Attention, name, text));
let line = crate::protection::os_sandbox_line(home, host)?;
report.human(Place::Agents, Row::new(Mark::Info, name, line));
}
Ok(hook_files)
}
fn location(
report: &mut Report,
host: Host,
config: &HostConfig,
recorded: &Recorded,
) -> Result<()> {
let name = host.display_name();
if let Some((locate, set_up)) = config.custom_location() {
if recorded.dir(host).is_some()
&& let Some(line) = crate::install::location::env_disagrees(locate, &set_up)?
{
report.line(Area::Hook, false, format!("{name:<16} {line}"));
}
} else if let (Some(env), Some(dir)) = (env_config_dir(host)?, recorded.dir(host))
&& env != dir
{
let (var, _) = dir_variable(host)?;
report.line(
Area::Hook,
false,
format!(
"{name:<16} {var} is {}, but `openmoat init` set it up in {}, which OpenMoat uses; \
run `openmoat init` with {var} set to set up that directory instead",
env.display(),
dir.display()
),
);
}
Ok(())
}
fn installed(report: &mut Report, home: &Home, config: &HostConfig, hook_files: &mut Vec<PathBuf>) {
let (host, name) = (config.host, config.host.display_name());
if let Some(warning) = config.trust_warning() {
let text = format!("{name:<16} {warning}");
match crate::install::trust_note::step(host) {
Some(step) => {
report.warn_as(&text, Row::new(Mark::Attention, name, warning).fix(step));
}
None => report.warn(&text),
}
}
hook_files.extend(config.extra_files());
hook_files.push(config.settings_path.clone());
let through = crate::install::host_version::hook_api(host)
.map(|through| format!(" ({through})"))
.unwrap_or_default();
report.line(
Area::Hook,
true,
format!(
"{name:<16} hook installed{through} {}",
config.settings_path.display()
),
);
if let Some(loaded) = crate::heartbeat::check(home, config) {
let (mark, line) = crate::heartbeat::line(host, &loaded);
let text = format!("{name:<16} {line}");
match (mark, crate::heartbeat::fix(host, &loaded)) {
(crate::heartbeat::Mark::Note | crate::heartbeat::Mark::Detail, _) => {
report.note(&text);
}
(crate::heartbeat::Mark::Warning, Some(fix)) => {
report.warn_as(
&format!("{text}. Fix: {fix}"),
Row::new(Mark::Attention, name, line).fix(fix),
);
}
(crate::heartbeat::Mark::Warning, None) => report.warn(&text),
}
}
if config.runs_calls_when_hook_fails() {
let programs: Vec<String> = host
.spec()
.process
.names
.iter()
.map(|n| format!("`{n}`"))
.collect();
report.warn(&format!(
"{name:<16} runs a tool call when its hook fails to start, crashes or \
times out: no {programs} {since} or later on PATH or in an editor extension, \
or an older copy beside it, so the hook has no `onFailure: \"block\"`; \
update {name}, then run `openmoat init`",
programs = programs.join(" or "),
since = crate::install::on_failure_since(host)
));
}
}
pub(super) fn pins(report: &mut Report, lock: &Lock, home: &Home, hook_files: &[PathBuf]) {
let gap = HookPinGap::new(lock, home, hook_files);
for path in gap.unpinned {
report.line(
Area::Hook,
false,
format!(
"hook file {} is not pinned by the lock; run `openmoat init` to pin it",
path.display()
),
);
}
for path in gap.elsewhere {
let (path, moved) = (path.display(), HookPinGap::moved_by());
report.note_at(
&format!(
"hook file {path} is pinned but not this shell's ({moved}); re-pinning keeps it"
),
Place::OpenMoat,
);
}
}