use std::{
env,
ffi::OsString,
fs,
path::{Path, PathBuf},
};
use allot_client::BlockingClient;
use allot_shared::{
api::HealthResponse,
config::Config,
paths::Paths,
platform::{ALLOT_BINARIES, CLI_EXE},
};
use anstyle::Style;
use anyhow::bail;
use serde::Serialize;
use crate::{
output::{BAD, GOOD, WAIT, paint, print_json},
registration::{self, State},
};
const REINSTALL: &str =
"run the one-line install again (README, Install), or `allot install` from a release folder";
const VERSION: &str = env!("CARGO_PKG_VERSION");
const NPM_LAUNCHER_ENV: &str = "ALLOT_NPM_LAUNCHER";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
enum Outcome {
Ok,
Warn,
Fail,
}
impl Outcome {
fn label(self) -> (&'static str, Style) {
match self {
Self::Ok => ("ok", GOOD),
Self::Warn => ("warn", WAIT),
Self::Fail => ("fail", BAD),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct Check {
name: &'static str,
outcome: Outcome,
detail: String,
fix: Option<String>,
}
impl Check {
fn ok(name: &'static str, detail: String) -> Self {
Self {
name,
outcome: Outcome::Ok,
detail,
fix: None,
}
}
fn problem(name: &'static str, outcome: Outcome, detail: String, fix: &str) -> Self {
Self {
name,
outcome,
detail,
fix: Some(fix.to_owned()),
}
}
}
pub fn doctor(json: bool) -> anyhow::Result<()> {
let paths = Paths::from_env()?;
let mut checks = vec![
check_binaries(&paths),
check_path(&paths, env::var_os(NPM_LAUNCHER_ENV)),
];
checks.extend(check_config_and_daemon(&paths));
checks.extend(
registration::inspect(&paths).map(|inspection| match inspection.state {
State::Done => Check::ok(inspection.name, inspection.detail),
State::Missing | State::Unknown => {
Check::problem(inspection.name, Outcome::Warn, inspection.detail, REINSTALL)
}
}),
);
if json {
print_json(&checks)?;
} else {
anstream::print!("{}", format_report(&checks));
}
let failed = checks
.iter()
.filter(|check| check.outcome == Outcome::Fail)
.count();
if failed > 0 {
bail!("{failed} check(s) failed");
}
Ok(())
}
fn format_report(checks: &[Check]) -> String {
let width = checks
.iter()
.map(|check| check.name.len())
.max()
.unwrap_or(0);
let mut text = String::new();
for check in checks {
let (label, style) = check.outcome.label();
text.push_str(&format!(
"{} {:<width$} {}\n",
paint(style, &format!("{label:<4}")),
check.name,
check.detail
));
if let Some(fix) = &check.fix {
text.push_str(&format!(" {:<width$} fix: {fix}\n", ""));
}
}
let problems = checks
.iter()
.filter(|check| check.outcome != Outcome::Ok)
.count();
if problems == 0 {
text.push_str("\nall checks passed\n");
}
text
}
fn check_binaries(paths: &Paths) -> Check {
let bin = paths.bin_dir();
let missing: Vec<&str> = ALLOT_BINARIES
.into_iter()
.filter(|name| !bin.join(name).is_file())
.collect();
if missing.is_empty() {
return Check::ok("binaries", format!("{} has all three", bin.display()));
}
Check::problem(
"binaries",
Outcome::Fail,
format!("{} lacks {}", bin.display(), missing.join(", ")),
REINSTALL,
)
}
fn check_path(paths: &Paths, npm_launcher: Option<OsString>) -> Check {
if let Some(launcher) = npm_launcher.filter(|value| !value.is_empty()) {
return Check::ok(
"path",
format!(
"allot runs through npm's launcher {}",
Path::new(&launcher).display()
),
);
}
let path = env::var_os("PATH").unwrap_or_default();
let dirs: Vec<PathBuf> = env::split_paths(&path).collect();
check_found(first_on_path(&dirs, CLI_EXE), paths)
}
fn check_found(found: Option<PathBuf>, paths: &Paths) -> Check {
let installed = paths.bin_dir().join(CLI_EXE);
match found {
Some(found) if is_this_install(&found, &installed) => {
Check::ok("path", format!("allot runs {}", found.display()))
}
Some(found) if same_contents(&found, &installed) => Check::ok(
"path",
format!("allot runs {}, a copy of this install", found.display()),
),
Some(found) => Check::problem(
"path",
Outcome::Warn,
format!("allot on PATH is {}, not this install", found.display()),
&format!(
"run `allot install` if that is a newer build; otherwise remove or rename it, or \
put {} first on PATH",
paths.bin_dir().display()
),
),
None => Check::problem(
"path",
Outcome::Warn,
"allot is not on PATH".to_owned(),
&format!("add {} to PATH", paths.bin_dir().display()),
),
}
}
fn first_on_path(dirs: &[PathBuf], name: &str) -> Option<PathBuf> {
dirs.iter()
.map(|dir| dir.join(name))
.find(|file| file.is_file())
}
fn is_this_install(found: &Path, installed: &Path) -> bool {
let canonical = |path: &Path| path.canonicalize().ok();
found == installed || canonical(found).is_some_and(|found| Some(found) == canonical(installed))
}
fn same_contents(a: &Path, b: &Path) -> bool {
matches!((fs::read(a), fs::read(b)), (Ok(a), Ok(b)) if a == b)
}
fn check_config_and_daemon(paths: &Paths) -> Vec<Check> {
let config = match Config::load(&paths.config_file()) {
Ok(_) => Check::ok(
"config",
format!("{} is valid or absent", paths.config_file().display()),
),
Err(error) => Check::problem(
"config",
Outcome::Fail,
error.to_string(),
"fix the file; allotd keeps the previous config until it is valid",
),
};
let client = match BlockingClient::discover(false) {
Ok(client) => client,
Err(error) => {
let daemon = Check::problem(
"allotd",
Outcome::Fail,
format!("{error:#}"),
"fix the config file, then `allot daemon start`",
);
return vec![config, daemon];
}
};
let daemon = match client.health() {
Ok(health) => check_running(&health, client.base_url()),
Err(error) if error.is_unreachable() => Check::problem(
"allotd",
Outcome::Warn,
format!("not running at {}", client.base_url()),
"`allot daemon start`; sessions also start it on their first heavy command",
),
Err(error) => Check::problem(
"allotd",
Outcome::Fail,
format!("{error:#}"),
"`allot daemon stop`, then `allot daemon start`",
),
};
vec![config, daemon]
}
fn check_running(health: &HealthResponse, url: &str) -> Check {
if let Some(error) = &health.config_error {
return Check::problem(
"allotd",
Outcome::Fail,
format!("allotd {} rejected the config: {error}", health.version),
"fix the config file; allotd reloads it by itself",
);
}
if health.version != VERSION {
return Check::problem(
"allotd",
Outcome::Warn,
format!(
"allotd {} runs at {url}, but this allot is {VERSION}",
health.version
),
"`allot daemon stop`, then `allot daemon start`, to run the installed build",
);
}
Check::ok(
"allotd",
format!(
"allotd {} running at {url} (pid {})",
health.version, health.pid
),
)
}
#[cfg(test)]
mod tests {
use std::{fs, process};
use super::*;
use crate::output::plain;
fn health(version: &str, config_error: Option<&str>) -> HealthResponse {
HealthResponse {
version: version.into(),
pid: 4_242,
uptime_secs: 5,
config_error: config_error.map(Into::into),
}
}
#[test]
fn test_check_running_flags_another_version_and_a_rejected_config() {
let url = "http://127.0.0.1:47800";
assert_eq!(
check_running(&health(VERSION, None), url).outcome,
Outcome::Ok
);
let old = check_running(&health("0.0.1", None), url);
assert_eq!(old.outcome, Outcome::Warn);
assert!(old.detail.contains("allotd 0.0.1 runs"), "{}", old.detail);
let rejected = check_running(&health(VERSION, Some("bad cores")), url);
assert_eq!(rejected.outcome, Outcome::Fail);
assert!(
rejected.detail.ends_with("bad cores"),
"{}",
rejected.detail
);
}
#[test]
fn test_first_on_path_takes_the_earliest_directory() {
let root = env::temp_dir().join(format!("allot-doctor-{}", process::id()));
let (first, second) = (root.join("a"), root.join("b"));
fs::create_dir_all(&first).unwrap();
fs::create_dir_all(&second).unwrap();
fs::write(second.join(CLI_EXE), "").unwrap();
let dirs = [root.join("missing"), first.clone(), second.clone()];
assert_eq!(first_on_path(&dirs, CLI_EXE), Some(second.join(CLI_EXE)));
fs::write(first.join(CLI_EXE), "").unwrap();
assert_eq!(first_on_path(&dirs, CLI_EXE), Some(first.join(CLI_EXE)));
assert!(is_this_install(
&first.join(CLI_EXE),
&first.join(".").join(CLI_EXE)
));
assert!(!is_this_install(
&first.join(CLI_EXE),
&second.join(CLI_EXE)
));
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn test_check_path_accepts_the_npm_launcher() {
let paths = Paths::under(env::temp_dir().join("allot-doctor-npm"));
let launcher = Path::new("npm").join("bin").join("allot.js");
let check = check_path(&paths, Some(launcher.clone().into_os_string()));
assert_eq!(check.outcome, Outcome::Ok);
assert!(
check.detail.ends_with(&launcher.display().to_string()),
"{}",
check.detail
);
assert_ne!(
check_path(&paths, Some(OsString::new())).detail,
check.detail
);
}
#[test]
fn test_check_found_accepts_only_an_identical_copy() {
let root = env::temp_dir().join(format!("allot-doctor-copy-{}", process::id()));
let paths = Paths::under(root.join("home"));
fs::create_dir_all(paths.bin_dir()).unwrap();
fs::write(paths.bin_dir().join(CLI_EXE), "build one").unwrap();
let cargo_bin = root.join("cargo");
fs::create_dir_all(&cargo_bin).unwrap();
let found = cargo_bin.join(CLI_EXE);
fs::write(&found, "build one").unwrap();
let copy = check_found(Some(found.clone()), &paths);
assert_eq!(copy.outcome, Outcome::Ok);
assert!(
copy.detail.contains("a copy of this install"),
"{}",
copy.detail
);
fs::write(&found, "build two").unwrap();
let other = check_found(Some(found), &paths);
assert_eq!(other.outcome, Outcome::Warn);
assert!(other.fix.unwrap().contains("allot install"));
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn test_format_report_puts_each_fix_under_its_check() {
let checks = [
Check::ok("binaries", "all three".into()),
Check::problem("path", Outcome::Warn, "not on PATH".into(), "add it"),
];
let text = plain(format_report(&checks));
assert_eq!(
text,
"ok binaries all three\nwarn path not on PATH\n fix: add \
it\n"
);
let healthy = plain(format_report(&checks[..1]));
assert!(healthy.ends_with("\nall checks passed\n"), "{healthy}");
}
#[test]
fn test_check_json_names_the_outcome_in_snake_case() {
let check = Check::problem("path", Outcome::Warn, "x".into(), "y");
assert_eq!(
serde_json::to_value(&check).unwrap(),
serde_json::json!({ "name": "path", "outcome": "warn", "detail": "x", "fix": "y" })
);
}
}