Skip to main content

cuqueclicker_lib/
self_cmd.rs

1//! `cuqueclicker self update` — re-install the latest released version in
2//! place. How it does that depends on how this binary was built:
3//!
4//! - **Shipped-binary build** (the `binary-release` cargo feature is enabled,
5//!   i.e. it came from a GitHub Release via curl+sh or PowerShell): re-runs
6//!   the same one-liner installer that initially fetched it. The installer
7//!   is idempotent — it overwrites the binary at the same install path.
8//! - **Source build** (the default, e.g. `cargo install` or a local
9//!   `cargo build`): runs `cargo install cuqueclicker --force`, which will
10//!   pick up the newest version from crates.io.
11//!
12//! Both paths inherit stdio, so the user sees installer / cargo output live.
13
14use anyhow::{Context, Result, bail};
15use std::process::Command;
16
17#[cfg(all(feature = "binary-release", unix))]
18const UNIX_INSTALL_URL: &str =
19    "https://raw.githubusercontent.com/flipbit03/cuqueclicker/main/install.sh";
20#[cfg(all(feature = "binary-release", windows))]
21const WINDOWS_INSTALL_URL: &str =
22    "https://raw.githubusercontent.com/flipbit03/cuqueclicker/main/install.ps1";
23
24#[cfg(feature = "binary-release")]
25pub fn update() -> Result<()> {
26    println!("Updating via installer script...");
27
28    #[cfg(unix)]
29    let status = Command::new("sh")
30        .arg("-c")
31        .arg(format!("curl -fsSL {UNIX_INSTALL_URL} | sh"))
32        .status()
33        .context("failed to spawn sh")?;
34
35    #[cfg(windows)]
36    let status = Command::new("powershell")
37        .args(["-Command", &format!("irm {WINDOWS_INSTALL_URL} | iex")])
38        .status()
39        .context("failed to spawn powershell")?;
40
41    if !status.success() {
42        bail!("update installer exited with status {:?}", status.code());
43    }
44    Ok(())
45}
46
47#[cfg(not(feature = "binary-release"))]
48pub fn update() -> Result<()> {
49    println!("Updating via `cargo install cuqueclicker --force`...");
50    let status = Command::new("cargo")
51        .args(["install", "cuqueclicker", "--force"])
52        .status()
53        .context("failed to spawn cargo (is it on your PATH?)")?;
54    if !status.success() {
55        bail!("cargo install exited with status {:?}", status.code());
56    }
57    Ok(())
58}