use super::Row;
use crate::boundary::dispatch::Deps;
use crate::wire::material::{self, ADDRESS, REMEDY, Role};
pub(super) fn rows(deps: &Deps) -> Vec<Row> {
let dir = material::dir(&deps.world);
vec![wire(&dir), address(&dir), listener(deps), git(deps)]
}
fn wire(dir: &std::path::Path) -> Row {
match material::read_dir(dir, Role::Server) {
Ok(Some(_)) => Row::ok(
"wire",
format!("{} holds a CA and this box's leaves", dir.display()),
),
Ok(None) => Row::bad(
"wire",
format!("{} holds no wire material", dir.display()),
format!(
"`{REMEDY}` mints it, and the engine's own boot mints it for a box that has none"
),
),
Err(half) => Row::bad(
"wire",
half,
format!(
"`FORCE=1 {REMEDY}` re-founds the directory; a rotation distrusts every \
certificate already issued"
),
),
}
}
fn address(dir: &std::path::Path) -> Row {
let stated = std::fs::read_to_string(dir.join(ADDRESS))
.unwrap_or_default()
.trim()
.to_owned();
let dialable = stated
.rsplit_once(':')
.is_some_and(|(host, port)| !host.is_empty() && !port.is_empty() && port != "0");
if dialable {
return Row::ok(
"address",
format!("{} names {stated}", dir.join(ADDRESS).display()),
);
}
Row::bad(
"address",
if stated.is_empty() {
format!("{} names no address", dir.join(ADDRESS).display())
} else {
format!(
"{stated} is a request, not an endpoint: only the listener learns what a `:0` \
became, and it becomes something else at the next boot"
)
},
format!(
"`WIRE_HOST=<host> WIRE_PORT=<port> {REMEDY}` states it over the CA already here, \
distrusting nothing — then restart the engine"
),
)
}
fn listener(deps: &Deps) -> Row {
match deps.caller.listening.address() {
Some(bound) => Row::ok("listener", format!("listening on {bound}")),
None => Row::bad(
"listener",
"this answer did not come from a listening engine".to_owned(),
"start `yog` — its boot says what it bound on stderr, and says there too when \
another process won the port"
.to_owned(),
),
}
}
fn git(deps: &Deps) -> Row {
let read = |key: &str| {
crate::git_env::output(
crate::git_env::git()
.env("HOME", &deps.home)
.env("GIT_CONFIG_GLOBAL", deps.home.join(".gitconfig"))
.args(["config", "--get", key]),
)
.ok()
.filter(|out| out.status.success())
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_owned())
.filter(|value| !value.is_empty())
};
match (read("user.name"), read("user.email")) {
(Some(name), Some(_)) => Row::ok("git", format!("commits are authored as {name}")),
_ => Row::bad(
"git",
"this box has no git identity".to_owned(),
"`git config --global user.name \"<name>\"` and `git config --global user.email \
<address>` — every workspace and every step is a commit"
.to_owned(),
),
}
}