use axoupdater::{AxoUpdater, AxoupdateError, Version};
use crate::exit::EXIT_ALBA_ERROR;
use crate::render::LineSink;
pub fn run() -> i32 {
let mut updater = AxoUpdater::new_for("alba");
match updater.load_receipt() {
Ok(_) => {}
Err(AxoupdateError::NoReceipt { .. }) => {
LineSink::stderr().line(
"this alba was not installed by the release installers, so it cannot update \
itself: run `cargo install alba` if that is how it was installed, or \
reinstall from https://github.com/albahq/alba/releases",
);
return EXIT_ALBA_ERROR;
}
Err(error) => return fail(&error),
}
let current = Version::parse(env!("CARGO_PKG_VERSION"))
.expect("CARGO_PKG_VERSION is a valid semver version");
if let Err(error) = updater.set_current_version(current.clone()) {
return fail(&error);
}
let runtime = match tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
{
Ok(runtime) => runtime,
Err(error) => return fail(&error),
};
match runtime.block_on(updater.run()) {
Ok(Some(update)) => {
LineSink::stdout().line(&format!(
"updated alba from {current} to {}",
update.new_version
));
0
}
Ok(None) => {
LineSink::stdout().line(&format!("alba {current} is already the latest release"));
0
}
Err(error) => fail(&error),
}
}
fn fail(error: &dyn std::fmt::Display) -> i32 {
LineSink::stderr().line(&format!("cannot update alba: {error}"));
EXIT_ALBA_ERROR
}