use std::path::{Path, PathBuf};
use std::time::Duration;
use anyhow::{Context, Result};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use crate::config::{Update, UpdateMode};
pub const NO_AUTOUPDATE_ENV: &str = "MAGI_NO_AUTOUPDATE";
pub fn default_interval() -> Duration {
kaishin::default_interval()
}
const MIN_INTERVAL: Duration = Duration::from_secs(60);
pub fn effective_interval(cfg: &Update) -> Duration {
let interval = cfg
.interval
.as_deref()
.and_then(|s| kaishin::parse_interval(s).ok())
.unwrap_or_else(default_interval);
interval.max(MIN_INTERVAL)
}
pub fn disabled_by_env() -> bool {
match std::env::var(NO_AUTOUPDATE_ENV) {
Ok(v) => {
let v = v.trim();
!(v.is_empty() || v == "0" || v.eq_ignore_ascii_case("false"))
}
Err(_) => false,
}
}
const OWNER: &str = "yukimemi";
const REPO: &str = "magi";
const BIN: &str = "magi";
const CRATE: &str = "magi-cli";
fn options() -> kaishin::KaishinOptions {
kaishin::KaishinOptions::new(OWNER, REPO, BIN, env!("CARGO_PKG_VERSION")).crate_name(CRATE)
}
fn state_path() -> Option<PathBuf> {
dirs::cache_dir().map(|d| d.join("magi").join("last_update_check.json"))
}
pub async fn run_self_update(yes: bool, check_only: bool, non_interactive: bool) -> Result<()> {
let opts = kaishin::UpdateOptions::new()
.yes(yes)
.check_only(check_only)
.non_interactive(non_interactive);
kaishin::run_self_update(&options(), opts).await
}
pub enum Pending {
Cached {
checker: Checker,
latest: kaishin::LatestRelease,
},
Notify {
checker: Checker,
handle: tokio::task::JoinHandle<Result<Option<kaishin::LatestRelease>>>,
},
Install {
handle: tokio::task::JoinHandle<Result<Option<kaishin::LatestRelease>>>,
},
}
#[derive(Clone)]
pub struct Checker {
inner: kaishin::Checker,
}
impl Checker {
pub fn new(cfg: &Update) -> Option<Self> {
if cfg.mode == UpdateMode::Off {
return None;
}
let mut inner = kaishin::Checker::new(BIN, options());
if let Some(path) = state_path() {
inner = inner.state_path(path);
}
Some(Self {
inner: inner.interval(effective_interval(cfg)),
})
}
pub fn should_check(&self) -> bool {
self.inner.should_check()
}
pub async fn newer_release(&self) -> Result<Option<kaishin::LatestRelease>> {
self.inner.check_and_save().await
}
pub fn cached_update(&self) -> Option<kaishin::LatestRelease> {
self.inner.cached_update()
}
pub fn format_banner(&self, latest: &kaishin::LatestRelease) -> String {
self.inner.format_banner(latest)
}
#[cfg(test)]
pub(crate) fn for_test(interval: Duration, state_path: PathBuf) -> Self {
let opts = kaishin::KaishinOptions::new(OWNER, REPO, BIN, env!("CARGO_PKG_VERSION"));
Self {
inner: kaishin::Checker::new(BIN, opts)
.state_path(state_path)
.interval(interval),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Stage {
Downloading,
Replaced,
Parking,
Restarting,
Done,
Failed,
}
impl Stage {
#[must_use]
pub fn terminal(self) -> bool {
matches!(self, Self::Done | Self::Failed)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Progress {
pub stage: Stage,
pub from: String,
pub to: Option<String>,
#[serde(default)]
pub parked_run: Option<String>,
pub started_at: Timestamp,
pub updated_at: Timestamp,
#[serde(default)]
pub detail: Option<String>,
}
impl Progress {
#[must_use]
pub fn new(from: String, to: String) -> Self {
let now = Timestamp::now();
Self {
stage: Stage::Downloading,
from,
to: Some(to),
parked_run: None,
started_at: now,
updated_at: now,
detail: None,
}
}
pub fn advance(&mut self, stage: Stage) {
self.stage = stage;
self.updated_at = Timestamp::now();
}
pub fn fail(&mut self, detail: impl Into<String>) {
self.stage = Stage::Failed;
self.updated_at = Timestamp::now();
self.detail = Some(detail.into());
}
}
#[must_use]
pub fn progress_path(home: &Path) -> PathBuf {
home.join("upgrade.json")
}
pub fn write_progress(home: &Path, progress: &Progress) -> Result<()> {
let path = progress_path(home);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
}
let body = serde_json::to_string_pretty(progress).context("serialize upgrade progress")?;
let tmp = path.with_extension("json.tmp");
std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
std::fs::rename(&tmp, &path).with_context(|| format!("replace {}", path.display()))?;
Ok(())
}
#[must_use]
pub fn read_progress(home: &Path) -> Option<Progress> {
let body = std::fs::read_to_string(progress_path(home)).ok()?;
serde_json::from_str(&body).ok()
}
pub fn reconcile_after_restart(home: &Path) {
let Some(mut progress) = read_progress(home) else {
return;
};
if progress.stage.terminal() {
return;
}
let running = env!("CARGO_PKG_VERSION");
if progress
.to
.as_deref()
.is_some_and(|to| to.trim_start_matches('v') == running)
{
progress.advance(Stage::Done);
} else {
let to = progress
.to
.clone()
.unwrap_or_else(|| "the expected release".to_owned());
progress.fail(format!(
"this process came up on {running}, not {to} - the upgrade may \
not have replaced the binary"
));
}
let _ = write_progress(home, &progress);
}
pub fn spawn(cfg: &Update, rt: &tokio::runtime::Handle) -> Option<Pending> {
if disabled_by_env() || cfg.mode == UpdateMode::Off {
return None;
}
let checker = Checker::new(cfg)?;
match cfg.mode {
UpdateMode::Off => None,
UpdateMode::Notify => {
if !checker.should_check() {
let latest = checker.cached_update()?;
return Some(Pending::Cached { checker, latest });
}
let inner = checker.inner.clone();
let handle = rt.spawn(async move { inner.check_and_save().await });
Some(Pending::Notify { checker, handle })
}
UpdateMode::Install => {
let inner = checker.inner.clone();
let handle = rt.spawn(async move { inner.auto_update().await });
Some(Pending::Install { handle })
}
}
}
pub async fn finalize(pending: Option<Pending>, budget: Duration) {
let Some(pending) = pending else {
return;
};
match pending {
Pending::Cached { checker, latest } => {
eprintln!("{}", checker.format_banner(&latest));
}
Pending::Notify { checker, handle } => {
if let Ok(Ok(Ok(Some(latest)))) = tokio::time::timeout(budget, handle).await {
eprintln!("{}", checker.format_banner(&latest));
}
}
Pending::Install { handle } => {
if let Ok(Ok(Ok(Some(latest)))) = tokio::time::timeout(budget, handle).await {
eprintln!("magi updated itself to {}", latest.tag_name);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn effective_interval_floors_a_configured_interval_below_githubs_rate_limit() {
let cfg = Update {
mode: UpdateMode::Notify,
interval: Some("1s".to_owned()),
};
assert_eq!(
effective_interval(&cfg),
MIN_INTERVAL,
"an interval that would exceed GitHub's rate limit under continuous \
polling must be floored rather than honoured verbatim"
);
let sane = Update {
mode: UpdateMode::Notify,
interval: Some("2h".to_owned()),
};
assert_eq!(
effective_interval(&sane),
Duration::from_secs(2 * 60 * 60),
"an interval already above the floor must pass through unchanged"
);
}
#[test]
fn env_kill_switch_semantics() {
unsafe {
std::env::remove_var(NO_AUTOUPDATE_ENV);
}
assert!(!disabled_by_env());
for (value, disabled) in [
("1", true),
("true", true),
("yes", true),
("0", false),
("false", false),
("FALSE", false),
("", false),
(" ", false),
] {
unsafe {
std::env::set_var(NO_AUTOUPDATE_ENV, value);
}
assert_eq!(
disabled_by_env(),
disabled,
"MAGI_NO_AUTOUPDATE={value:?} should {} disable",
if disabled { "" } else { "not" }
);
}
unsafe {
std::env::remove_var(NO_AUTOUPDATE_ENV);
}
}
#[test]
fn off_mode_never_spawns() {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let cfg = Update {
mode: UpdateMode::Off,
interval: None,
};
assert!(spawn(&cfg, rt.handle()).is_none());
}
#[test]
fn state_path_lives_under_the_cache_dir() {
let path = state_path().expect("a cache dir on every supported platform");
assert!(path.ends_with("magi/last_update_check.json"));
let data = dirs::data_local_dir().unwrap_or_default();
assert!(
!path.starts_with(&data) || dirs::cache_dir() == dirs::data_local_dir(),
"throttle state must not sit in the run history directory"
);
}
#[tokio::test]
async fn finalize_of_nothing_is_a_no_op() {
finalize(None, Duration::from_millis(1)).await;
}
#[test]
fn checking_is_off_for_every_caller_when_the_config_says_off() {
assert!(
Checker::new(&Update {
mode: UpdateMode::Off,
interval: None,
})
.is_none(),
"an operator who writes mode = \"off\" means it"
);
for mode in [UpdateMode::Notify, UpdateMode::Install] {
assert!(
Checker::new(&Update {
mode,
interval: None,
})
.is_some(),
"{mode:?} still asks the forge"
);
}
}
#[test]
fn cached_update_answers_from_disk_with_no_network_call() {
let dir = tempfile::tempdir().expect("temp dir");
let path = dir.path().join("state.json");
let opts = kaishin::KaishinOptions::new("yukimemi", "magi", "magi", "0.1.0");
let checker = Checker {
inner: kaishin::Checker::new("magi", opts).state_path(path.clone()),
};
assert!(
checker.cached_update().is_none(),
"no state file yet must read as \"unknown\", not an error"
);
let state = kaishin::UpdateCheckState {
last_checked_unix: 0,
last_known_latest: Some("v9.9.9".to_owned()),
last_known_url: Some("https://example.invalid/9.9.9".to_owned()),
};
kaishin::save_check_state(&path, &state).expect("seed the state file");
let latest = checker.cached_update().expect("a newer release was cached");
assert_eq!(latest.tag_name, "v9.9.9");
}
#[test]
fn reconcile_after_restart_confirms_a_matching_version() {
let home = tempfile::tempdir().expect("temp home");
let mut progress = Progress::new(
"0.1.0".to_owned(),
format!("v{}", env!("CARGO_PKG_VERSION")),
);
progress.advance(Stage::Restarting);
write_progress(home.path(), &progress).expect("seed progress");
reconcile_after_restart(home.path());
let after = read_progress(home.path()).expect("progress on disk");
assert_eq!(
after.stage,
Stage::Done,
"the successor is running exactly the release that was asked for, \
`v` prefix and all"
);
}
#[test]
fn reconcile_after_restart_flags_a_mismatched_version() {
let home = tempfile::tempdir().expect("temp home");
let mut progress = Progress::new("0.1.0".to_owned(), "v9.9.9".to_owned());
progress.advance(Stage::Restarting);
write_progress(home.path(), &progress).expect("seed progress");
reconcile_after_restart(home.path());
let after = read_progress(home.path()).expect("progress on disk");
assert_eq!(after.stage, Stage::Failed);
assert!(
after.detail.is_some_and(|d| d.contains("9.9.9")),
"the operator needs to know which release it did not come back on"
);
}
#[test]
fn reconcile_after_restart_leaves_a_settled_record_alone() {
let home = tempfile::tempdir().expect("temp home");
let mut progress = Progress::new("0.1.0".to_owned(), "9.9.9".to_owned());
progress.advance(Stage::Done);
write_progress(home.path(), &progress).expect("seed progress");
reconcile_after_restart(home.path());
let after = read_progress(home.path()).expect("progress on disk");
assert_eq!(
after.stage,
Stage::Done,
"an already-settled record must not be rewritten by a later, unrelated start"
);
}
#[test]
fn reconcile_after_restart_with_nothing_on_disk_is_a_quiet_no_op() {
let home = tempfile::tempdir().expect("temp home");
reconcile_after_restart(home.path());
assert!(read_progress(home.path()).is_none());
}
}