workhelix_cli_common/
update.rs1use crate::types::RepoInfo;
12use std::path::Path;
13use std::process::Command;
14
15#[must_use]
32pub fn run_update(
33 repo_info: &RepoInfo,
34 _current_version: &str,
35 version: Option<&str>,
36 force: bool,
37 install_dir: Option<&Path>,
38) -> i32 {
39 if version.is_some() {
40 eprintln!("⚠️ Specific version installation not yet supported");
41 eprintln!(" The install script will install the latest version");
42 println!();
43 }
44
45 println!("🔄 Running installation script...");
46 println!();
47
48 let install_script_url = format!(
50 "https://raw.githubusercontent.com/{}/{}/main/install.sh",
51 repo_info.owner, repo_info.name
52 );
53
54 let mut cmd = Command::new("sh");
56 cmd.arg("-c");
57
58 let mut env_vars = Vec::new();
60 env_vars.push(format!("REPO_OWNER={}", repo_info.owner));
61 env_vars.push(format!("REPO_NAME={}", repo_info.name));
62
63 if force {
64 env_vars.push("FORCE_INSTALL=1".to_string());
65 }
66
67 if let Some(dir) = install_dir {
68 env_vars.push(format!("INSTALL_DIR={}", dir.display()));
69 }
70
71 let env_string = env_vars.join(" ");
72 let command_string = format!("{env_string} curl -fsSL {install_script_url} | sh");
73
74 cmd.arg(&command_string);
75
76 match cmd.status() {
78 Ok(status) => {
79 if status.success() {
80 0
81 } else {
82 status.code().unwrap_or(1)
83 }
84 }
85 Err(e) => {
86 eprintln!("❌ Failed to run install script: {e}");
87 eprintln!(" Make sure curl is installed and you have internet access");
88 1
89 }
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_repo_info_latest_release_url() {
99 let repo = RepoInfo::new("workhelix", "prompter", "prompter-v");
100 let url = repo.latest_release_url();
101 assert_eq!(
102 url,
103 "https://api.github.com/repos/workhelix/prompter/releases/latest"
104 );
105 }
106
107 #[test]
108 fn test_install_script_url_construction() {
109 let repo = RepoInfo::new("tftio", "peter-hook", "v");
110 let expected = "https://raw.githubusercontent.com/tftio/peter-hook/main/install.sh";
111 let actual = format!(
112 "https://raw.githubusercontent.com/{}/{}/main/install.sh",
113 repo.owner, repo.name
114 );
115 assert_eq!(actual, expected);
116 }
117}