relay_knowledge/application/update/workflow/
mod.rs1use std::time::{SystemTime, UNIX_EPOCH};
2
3use crate::{
4 paths::RuntimePaths, ports::release_metadata::ReleaseMetadataPort, project::PROJECT_NAME,
5};
6
7use super::{
8 cache::{read_fresh_cache, write_cache},
9 config::UpdateRuntimeConfig,
10 release::fetch_latest_version,
11 result::VersionCheckResponse,
12};
13
14pub async fn check_for_updates(
15 paths: &RuntimePaths,
16 metadata: &dyn ReleaseMetadataPort,
17 config: &UpdateRuntimeConfig,
18 force_refresh: bool,
19) -> VersionCheckResponse {
20 let now_ms = current_time_millis();
21 let cache_path = paths.version_check_cache_file();
22 if !force_refresh
23 && let Some(cached) =
24 read_fresh_cache(&cache_path, now_ms, config.check_interval, config).await
25 {
26 return cached;
27 }
28
29 let response = fetch_latest_version(metadata, config, now_ms).await;
30 let _ = write_cache(&cache_path, &response, config).await;
31 response
32}
33
34pub async fn update_notice(
35 paths: &RuntimePaths,
36 metadata: &dyn ReleaseMetadataPort,
37 config: &UpdateRuntimeConfig,
38) -> Option<String> {
39 if !config.enabled {
40 return None;
41 }
42 let response = check_for_updates(paths, metadata, config, false).await;
43 notice_from_response(response)
44}
45
46fn notice_from_response(response: VersionCheckResponse) -> Option<String> {
47 if !response.update_available {
48 return None;
49 }
50
51 Some(format!(
52 "{} {} is available; current {}. Run `relay-knowledge version check` for details.\n",
53 PROJECT_NAME,
54 response
55 .latest_version
56 .unwrap_or_else(|| "unknown".to_owned()),
57 response.current_version
58 ))
59}
60
61fn current_time_millis() -> u64 {
62 SystemTime::now()
63 .duration_since(UNIX_EPOCH)
64 .unwrap_or_default()
65 .as_millis()
66 .try_into()
67 .unwrap_or(u64::MAX)
68}
69
70#[cfg(test)]
71#[path = "mod_tests.rs"]
72mod tests;