Skip to main content

kaizen/shell/
upgrade.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2use anyhow::{Result, bail};
3use std::process::Command;
4
5pub fn cmd_upgrade() -> Result<()> {
6    let (cmd, args) = detect_upgrade_command()?;
7    println!("Running: {} {}", cmd, args.join(" "));
8    let status = Command::new(cmd).args(&args).status()?;
9    if !status.success() {
10        bail!("{} exited with status {}", cmd, status);
11    }
12    Ok(())
13}
14
15fn detect_upgrade_command() -> Result<(&'static str, Vec<&'static str>)> {
16    let exe = std::env::current_exe()?;
17    let path = exe.to_string_lossy().into_owned();
18    if is_homebrew_install(&path) {
19        Ok(("brew", vec!["upgrade", "kaizen-cli"]))
20    } else {
21        Ok((
22            "cargo",
23            vec!["install", "kaizen-cli", "--locked", "--force"],
24        ))
25    }
26}
27
28fn is_homebrew_install(path: &str) -> bool {
29    path.contains("/Cellar/kaizen-cli")
30        || path.contains("/opt/homebrew/")
31        || path.contains("/usr/local/Cellar/")
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn cellar_path_is_homebrew() {
40        assert!(is_homebrew_install(
41            "/usr/local/Cellar/kaizen-cli/1.0/bin/kaizen"
42        ));
43    }
44
45    #[test]
46    fn opt_homebrew_path_is_homebrew() {
47        assert!(is_homebrew_install("/opt/homebrew/bin/kaizen"));
48    }
49
50    #[test]
51    fn cargo_home_path_is_not_homebrew() {
52        assert!(!is_homebrew_install("/Users/me/.cargo/bin/kaizen"));
53    }
54}