Skip to main content

flodl_cli/util/
install_prompt.rs

1//! Post-init / post-setup helper that offers to install `fdl` globally
2//! (`~/.local/bin/fdl`) so the user can drop the `./` prefix in
3//! subsequent invocations.
4//!
5//! Deliberately conservative: it skips itself when the current binary
6//! already lives at the target path, when a different `fdl` already
7//! exists there, or when no `HOME` is available. Users who decline
8//! the prompt get a one-line reminder.
9
10use std::path::PathBuf;
11use std::process::Command;
12
13use crate::util::prompt;
14
15/// Offer to install the running `fdl` binary into `~/.local/bin/fdl`.
16///
17/// Returns silently (no prompt, no output) when the offer is not
18/// applicable — see the module-level doc for the skip conditions.
19pub fn offer_global_install() {
20    let Some(home_os) = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"))
21    else {
22        return;
23    };
24    let target = PathBuf::from(home_os).join(".local/bin/fdl");
25
26    let current = match std::env::current_exe() {
27        Ok(p) => p,
28        Err(_) => return,
29    };
30
31    // Already running from the target path -> already installed.
32    let current_canon = current.canonicalize().unwrap_or_else(|_| current.clone());
33    let target_canon = target.canonicalize().unwrap_or_else(|_| target.clone());
34    if current_canon == target_canon {
35        return;
36    }
37
38    // Something else already occupies ~/.local/bin/fdl. Don't clobber it;
39    // the user can `./fdl install` explicitly if they want to override.
40    if target.exists() {
41        return;
42    }
43
44    println!();
45    let msg = format!(
46        "Install fdl globally to {}?",
47        target.display()
48    );
49    if !prompt::ask_yn(&msg, true) {
50        println!("  (later: ./fdl install)");
51        return;
52    }
53
54    let status = Command::new(&current).arg("install").status();
55    match status {
56        Ok(s) if s.success() => {}
57        _ => {
58            eprintln!("fdl install did not complete; rerun manually with `./fdl install`.");
59        }
60    }
61}