use anyhow::{Context, Result};
pub fn detect_cli_version(bin: &str, cache_file: &str, fallback: &str) -> String {
if let Some(v) = read_cached_version(cache_file) {
return v;
}
if let Some(v) = run_cli_version(bin) {
let _ = write_cached_version(cache_file, &v);
return v;
}
fallback.to_string()
}
pub fn parse_version(raw: &str) -> Option<String> {
raw.split_whitespace()
.find(|t| t.starts_with(|c: char| c.is_ascii_digit()) && t.contains('.'))
.map(str::to_string)
}
fn read_cached_version(cache_file: &str) -> Option<String> {
let path = crate::utils::get_cache_dir().ok()?.join(cache_file);
let text = std::fs::read_to_string(path).ok()?;
let v: serde_json::Value = serde_json::from_str(&text).ok()?;
if !is_today_utc(v.get("last_checked_at")?.as_str()?) {
return None;
}
v.get("version")?.as_str().map(str::to_string)
}
fn write_cached_version(cache_file: &str, version: &str) -> Result<()> {
let path = crate::utils::get_cache_dir()?.join(cache_file);
crate::utils::write_json_atomic(
path,
&serde_json::json!({
"version": version,
"last_checked_at": crate::utils::now_rfc3339_utc_nanos(),
}),
)
}
fn run_cli_version(bin: &str) -> Option<String> {
let output = std::process::Command::new(bin)
.arg("--version")
.output()
.ok()?;
output.status.success().then_some(())?;
parse_version(&String::from_utf8_lossy(&output.stdout))
}
fn is_today_utc(ts: &str) -> bool {
chrono::DateTime::parse_from_rfc3339(ts)
.map(|dt| dt.with_timezone(&chrono::Utc).date_naive() == chrono::Utc::now().date_naive())
.unwrap_or(false)
}
pub fn build_client() -> Result<reqwest::blocking::Client> {
reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(8))
.build()
.context("Failed to build HTTP client")
}
pub fn iso_to_unix_secs(s: &str) -> Option<i64> {
let ms = crate::utils::parse_iso_timestamp(s);
(ms > 0).then_some(ms / 1000)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn iso_to_unix_secs_handles_bad_input() {
assert_eq!(iso_to_unix_secs("not-a-date"), None);
assert!(iso_to_unix_secs("2026-07-03T17:09:59.651608+00:00").unwrap() > 0);
}
#[test]
fn is_today_utc_matches_now_but_not_a_past_day() {
let now = crate::utils::now_rfc3339_utc_nanos();
assert!(is_today_utc(&now));
assert!(!is_today_utc("2000-01-01T00:00:00Z"));
assert!(!is_today_utc("not-a-timestamp"));
}
#[test]
fn parse_version_handles_leading_or_trailing_labels() {
assert_eq!(
parse_version("2.1.201 (Claude Code)").as_deref(),
Some("2.1.201")
);
assert_eq!(
parse_version("codex-cli 0.142.5").as_deref(),
Some("0.142.5")
);
assert_eq!(parse_version(" 2.0.14\n").as_deref(), Some("2.0.14"));
assert_eq!(parse_version(""), None);
assert_eq!(parse_version("Claude Code"), None);
}
}