allot-cli 0.3.0

CLI for Allot: status, leases, history, daemon control, install, and the PreToolUse hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! `allot doctor`: checks the install without changing anything, and says what to do about
//! each problem.

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},
};

/// The fix for anything `allot install` sets up: running it again.
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");
/// Set by the npm package's launcher (`npm/bin/allot.js`) on the allot it runs.
const NPM_LAUNCHER_ENV: &str = "ALLOT_NPM_LAUNCHER";

/// How one check came out.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
enum Outcome {
    Ok,
    /// Works, but not as it should, or could not be told.
    Warn,
    /// Allot cannot work this way.
    Fail,
}

impl Outcome {
    fn label(self) -> (&'static str, Style) {
        match self {
            Self::Ok => ("ok", GOOD),
            Self::Warn => ("warn", WAIT),
            Self::Fail => ("fail", BAD),
        }
    }
}

/// One line of the report; `--json` prints a list of these.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct Check {
    name: &'static str,
    outcome: Outcome,
    detail: String,
    /// What to do; `None` when there is nothing to do.
    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()),
        }
    }
}

/// Checks the binaries, `PATH`, the config, allotd, and what `allot install` registers, prints
/// the report as text or JSON, and fails when a check failed.
///
/// # Errors
///
/// Fails when the home directory is unknown, or after the report when a check failed.
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));
    // Warnings, not failures: `--no-claude` and `--no-autostart` leave these out on purpose.
    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,
    )
}

/// `npm_launcher` is [`NPM_LAUNCHER_ENV`]'s value.
fn check_path(paths: &Paths, npm_launcher: Option<OsString>) -> Check {
    // npm's `allot` is a launcher for the binary in its own package, so on PATH it is never this
    // install's file, and on Windows it is a .cmd that the lookup below cannot see.
    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)
}

/// `found` is the first `allot` on PATH.
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()))
        }
        // `cargo install` leaves its copy in ~/.cargo/bin, usually first on PATH.
        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()),
        ),
    }
}

/// The first `name` in `dirs`, as a shell would find it.
fn first_on_path(dirs: &[PathBuf], name: &str) -> Option<PathBuf> {
    dirs.iter()
        .map(|dir| dir.join(name))
        .find(|file| file.is_file())
}

/// Whether `found` is `installed`, directly or through a link such as `~/.local/bin/allot`.
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))
}

/// Whether both files can be read and hold the same bytes.
fn same_contents(a: &Path, b: &Path) -> bool {
    matches!((fs::read(a), fs::read(b)), (Ok(a), Ok(b)) if a == b)
}

/// The config file, then allotd: whether it runs, its version, and whether it took the config.
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" })
        );
    }
}