use time::OffsetDateTime;
use troll_core::{Role, SilentEntry, drift};
use troll_probe::{Kind, ListenerProbe};
use super::{Context, inspect};
use crate::{
error::CliError,
output::{self, DriftOutput, SCHEMA, TaggedSilentEntry},
};
pub(crate) fn build_drift(
context: &Context,
kind: Option<Kind>,
role: Option<Role>,
) -> Result<DriftOutput, CliError> {
let registry = context.store.load()?;
let probe = context.probe()?;
let scan = probe.report()?;
let listeners = inspect::observe(context, probe, scan.listeners, None)?;
let pairs: Vec<_> = listeners.iter().map(|s| (s.port, s.bind)).collect();
let mut report = drift(®istry, &pairs, OffsetDateTime::now_utc());
report.claimed_silent.retain(|entry| match entry {
SilentEntry::Permanent(e) => role.is_none_or(|r| r == e.role),
SilentEntry::Lease(_) => true,
});
let unclaimed = listeners
.into_iter()
.filter(|s| {
report.listening_unclaimed.contains(&(s.port, s.bind))
&& kind.is_none_or(|k| k == s.kind)
})
.collect::<Vec<_>>();
let mut counts = report.counts();
counts.listening_unclaimed = unclaimed.len();
Ok(DriftOutput {
schema: SCHEMA,
complete: scan.skipped_rows == 0,
skipped_rows: scan.skipped_rows,
listening_unclaimed: unclaimed,
claimed_silent: report.claimed_silent.into_iter().map(Into::into).collect(),
leases_expiring: report.leases_expiring.into_iter().map(Into::into).collect(),
zone_conflicts: report.zone_conflicts,
counts,
})
}
pub(super) fn run(
context: &Context,
kind: Option<Kind>,
role: Option<Role>,
json: bool,
style: crate::style::Style,
) -> Result<(), CliError> {
let result = build_drift(context, kind, role)?;
output::write(json, &result, |out| {
output::write_listener_scan_warning(out, result.skipped_rows)?;
writeln!(out, "{}", style.heading("Listening but unclaimed"))?;
for s in &result.listening_unclaimed {
writeln!(
out,
" {} {} ({})",
style.accent(&s.port.to_string()),
style.dim(&s.bind.to_string()),
output::kind_name(s.kind)
)?;
}
writeln!(out, "\n{}", style.heading("Claimed but silent"))?;
for e in &result.claimed_silent {
match e {
TaggedSilentEntry::Permanent(claim) => {
let e = &claim.entry;
write!(
out,
" {} {} {} (permanent) {}",
style.accent(&e.port.to_string()),
style.dim(&e.bind.to_string()),
crate::style::display(&e.project),
style.dim(&format!("tag {}", claim.tag))
)?;
if let Some(link) = &claim.link {
write!(out, " {}", style.accent(link))?;
}
writeln!(out)?;
}
TaggedSilentEntry::Lease(lease) => {
let e = &lease.entry;
writeln!(
out,
" {} {} {} (lease) {}",
style.accent(&e.port.to_string()),
style.dim(&e.bind.to_string()),
crate::style::display(&e.project),
style.dim(&format!("tag {}", lease.tag))
)?;
}
}
}
writeln!(out, "\n{}", style.heading("Leases expiring within 2 hours"))?;
for lease in &result.leases_expiring {
let e = &lease.entry;
writeln!(
out,
" {} {} {} until {} {}",
style.accent(&e.port.to_string()),
style.dim(&e.bind.to_string()),
crate::style::display(&e.project),
output::timestamp(e.expires_at)?,
style.dim(&format!("tag {}", lease.tag))
)?;
}
writeln!(out, "\n{}", style.heading("Zone conflicts"))?;
for conflict in &result.zone_conflicts {
writeln!(out, " {conflict}")?;
}
Ok(())
})
}