mirror of https://github.com/buster-so/buster.git
just get rid of weird version caching
This commit is contained in:
parent
1a4e43b0d9
commit
d453d5a92b
|
@ -38,12 +38,21 @@ impl UpdateCommand {
|
|||
println!("Current version: {}", current_version);
|
||||
println!("Latest version: {}", latest_version);
|
||||
|
||||
if env::var("BUSTER_DEBUG").is_ok() {
|
||||
println!("Debug: Checking GitHub API URL: {}", GITHUB_RELEASES_URL);
|
||||
}
|
||||
|
||||
let update_available = super::version::is_update_available(current_version, &latest_version);
|
||||
|
||||
if !update_available && !self.force {
|
||||
if !update_available {
|
||||
if self.force {
|
||||
println!("\n⚠️ Warning: The latest version ({}) is older than your current version ({})", latest_version, current_version);
|
||||
println!(" Continuing due to --force flag...");
|
||||
} else {
|
||||
println!("\n{}", "You are using the latest version".green());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
if self.check_only {
|
||||
if update_available {
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
use anyhow::Result;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use dirs::cache_dir;
|
||||
use serde::Deserialize;
|
||||
|
||||
const GITHUB_API_URL: &str = "https://api.github.com/repos/buster-so/buster/releases/latest";
|
||||
const CACHE_TTL: Duration = Duration::from_secs(3600); // 1 hour
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GitHubRelease {
|
||||
|
@ -15,17 +10,7 @@ struct GitHubRelease {
|
|||
body: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct VersionCache {
|
||||
version: String,
|
||||
timestamp: u64,
|
||||
}
|
||||
|
||||
pub async fn check_latest_version() -> Result<Option<String>> {
|
||||
if let Some(cached_version) = get_cached_version()? {
|
||||
return Ok(Some(cached_version));
|
||||
}
|
||||
|
||||
let client = Client::new();
|
||||
let response = client
|
||||
.get(GITHUB_API_URL)
|
||||
|
@ -34,65 +19,9 @@ pub async fn check_latest_version() -> Result<Option<String>> {
|
|||
.await?;
|
||||
|
||||
let release: GitHubRelease = response.json().await?;
|
||||
cache_version(&release.tag_name)?;
|
||||
|
||||
Ok(Some(release.tag_name))
|
||||
}
|
||||
|
||||
fn get_cache_path() -> Option<PathBuf> {
|
||||
let mut cache_path = cache_dir()?;
|
||||
cache_path.push("buster");
|
||||
cache_path.push("version_check.json");
|
||||
Some(cache_path)
|
||||
}
|
||||
|
||||
fn get_cached_version() -> Result<Option<String>> {
|
||||
let cache_path = match get_cache_path() {
|
||||
Some(path) => path,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
if !cache_path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let cache_content = fs::read_to_string(cache_path)?;
|
||||
let cached: VersionCache = serde_json::from_str(&cache_content)?;
|
||||
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)?
|
||||
.as_secs();
|
||||
|
||||
if now - cached.timestamp > CACHE_TTL.as_secs() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(Some(cached.version))
|
||||
}
|
||||
|
||||
fn cache_version(version: &str) -> Result<()> {
|
||||
let cache_path = match get_cache_path() {
|
||||
Some(path) => path,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
if let Some(parent) = cache_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let cache = VersionCache {
|
||||
version: version.to_string(),
|
||||
timestamp: SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)?
|
||||
.as_secs(),
|
||||
};
|
||||
|
||||
let cache_content = serde_json::to_string(&cache)?;
|
||||
fs::write(cache_path, cache_content)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_update_available(current: &str, latest: &str) -> bool {
|
||||
// Strip 'v' prefix if present
|
||||
let current = current.trim_start_matches('v');
|
||||
|
|
Loading…
Reference in New Issue