#[derive(Debug, PartialEq, Eq)]
pub enum InstallMethod {
Brew,
Curl,
Unknown,
}
pub fn detect() -> InstallMethod {
let exe = std::env::current_exe().ok().map(resolve_links);
let marker = crate::config::config_dir()
.ok()
.and_then(|dir| std::fs::read_to_string(dir.join("install-method")).ok());
detect_from(
exe.as_deref().map(|p| p.to_string_lossy()).as_deref(),
marker.as_deref(),
)
}
fn detect_from(exe: Option<&str>, marker: Option<&str>) -> InstallMethod {
if exe.is_some_and(is_brew_path) {
return InstallMethod::Brew;
}
match marker.map(str::trim) {
Some("brew") => InstallMethod::Brew,
Some("curl") => InstallMethod::Curl,
_ => InstallMethod::Unknown,
}
}
fn resolve_links(path: std::path::PathBuf) -> std::path::PathBuf {
std::fs::canonicalize(&path).unwrap_or(path)
}
fn is_brew_path(path: &str) -> bool {
path.contains("/Cellar/")
}
pub fn binary_upgrade_command(method: &InstallMethod) -> String {
match method {
InstallMethod::Brew => "brew upgrade cx".to_string(),
InstallMethod::Curl => "curl -fsSL https://get.coralogix.dev/cli | sh".to_string(),
InstallMethod::Unknown => "https://github.com/coralogix/cx-cli#install".to_string(),
}
}
pub fn skills_upgrade_command() -> &'static str {
"cx skills install --global"
}
pub fn install_docs_url() -> &'static str {
"https://github.com/coralogix/cx-cli#install"
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn brew_upgrade_command() {
let cmd = binary_upgrade_command(&InstallMethod::Brew);
assert_eq!(cmd, "brew upgrade cx");
}
#[test]
fn curl_upgrade_command() {
let cmd = binary_upgrade_command(&InstallMethod::Curl);
assert!(cmd.contains("curl") && cmd.contains("coralogix.dev"));
}
#[test]
fn unknown_upgrade_command_is_url() {
let cmd = binary_upgrade_command(&InstallMethod::Unknown);
assert_eq!(cmd, "https://github.com/coralogix/cx-cli#install");
}
#[test]
fn skills_upgrade_command_is_global_install() {
assert_eq!(skills_upgrade_command(), "cx skills install --global");
}
#[test]
fn brew_binary_wins_over_curl_marker() {
let m = detect_from(
Some("/opt/homebrew/Cellar/cx/0.1.28/bin/cx"),
Some("curl\n"),
);
assert_eq!(m, InstallMethod::Brew);
}
#[test]
fn script_binary_uses_curl_marker() {
let m = detect_from(Some("/Users/me/.local/bin/cx"), Some("curl"));
assert_eq!(m, InstallMethod::Curl);
}
#[test]
fn linuxbrew_binary_without_marker_is_brew() {
let m = detect_from(
Some("/home/linuxbrew/.linuxbrew/Cellar/cx/0.1.28/bin/cx"),
None,
);
assert_eq!(m, InstallMethod::Brew);
}
#[test]
fn unknown_without_marker_or_brew_path() {
assert_eq!(
detect_from(Some("/usr/bin/cx"), None),
InstallMethod::Unknown
);
assert_eq!(detect_from(None, Some("other")), InstallMethod::Unknown);
}
#[test]
fn intel_homebrew_cellar_path_is_brew() {
let m = detect_from(Some("/usr/local/Cellar/cx/0.1.28/bin/cx"), Some("curl"));
assert_eq!(m, InstallMethod::Brew);
}
#[cfg(unix)]
#[test]
fn link_into_cellar_resolves_to_brew() {
let root = std::env::temp_dir().join(format!("cx-install-method-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let real_dir = root.join("Cellar/cx/0.1.28/bin");
let bin_dir = root.join("bin");
std::fs::create_dir_all(&real_dir).unwrap();
std::fs::create_dir_all(&bin_dir).unwrap();
std::fs::write(real_dir.join("cx"), "").unwrap();
std::os::unix::fs::symlink("../Cellar/cx/0.1.28/bin/cx", bin_dir.join("cx")).unwrap();
let link = bin_dir.join("cx");
assert!(!is_brew_path(&link.to_string_lossy()));
let resolved = resolve_links(link);
let m = detect_from(Some(&resolved.to_string_lossy()), Some("curl"));
let _ = std::fs::remove_dir_all(&root);
assert_eq!(m, InstallMethod::Brew);
}
#[test]
fn script_copy_in_homebrew_folder_uses_marker() {
let m = detect_from(Some("/opt/homebrew/bin/cx"), Some("curl"));
assert_eq!(m, InstallMethod::Curl);
}
}