1pub mod download;
3pub mod plan;
4
5use anyhow::{Context, Result, bail};
6use std::path::Path;
7use std::process::Command;
8
9use plan::{UpgradeAction, upgrade_action_for};
10
11pub fn cmd_upgrade(from_source: bool) -> Result<()> {
12 match upgrade_action_for_current_exe(from_source)? {
13 UpgradeAction::Homebrew => run_command("brew", &["upgrade", "kaizen-cli"]),
14 UpgradeAction::SourceCargo => {
15 run_command("cargo", &["install", "kaizen-cli", "--locked", "--force"])
16 }
17 UpgradeAction::ReleaseBinary => download::install_latest_release(),
18 }
19}
20
21fn upgrade_action_for_current_exe(from_source: bool) -> Result<UpgradeAction> {
22 let exe = std::env::current_exe().context("detect current executable")?;
23 Ok(upgrade_action_for(&exe, from_source))
24}
25
26fn run_command(cmd: &str, args: &[&str]) -> Result<()> {
27 println!("Running: {} {}", cmd, args.join(" "));
28 let status = Command::new(cmd).args(args).status()?;
29 if !status.success() {
30 bail!("{cmd} exited with status {status}");
31 }
32 Ok(())
33}
34
35pub fn is_homebrew_install(path: &Path) -> bool {
36 let path = path.to_string_lossy();
37 path.contains("/Cellar/kaizen-cli")
38 || path.contains("/opt/homebrew/")
39 || path.contains("/usr/local/Cellar/")
40}