Files
topgrade/src/self_update.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2018-12-11 16:43:26 +02:00
use super::error::{Error, ErrorKind};
2018-12-05 11:34:08 +02:00
use super::terminal::*;
2018-12-11 16:43:26 +02:00
use failure::ResultExt;
2018-11-26 14:27:19 +02:00
use self_update_crate;
2018-12-23 13:17:53 +02:00
use self_update_crate::backends::github::{GitHubUpdateStatus, Update};
2018-11-26 14:39:56 +02:00
#[cfg(unix)]
2018-11-26 14:27:19 +02:00
use std::env;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(unix)]
use std::process::Command;
2018-12-05 11:34:08 +02:00
pub fn self_update() -> Result<(), Error> {
print_separator("Self update");
2018-11-26 14:27:19 +02:00
#[cfg(unix)]
let current_exe = env::current_exe();
2018-12-11 16:43:26 +02:00
let target = self_update_crate::get_target().context(ErrorKind::SelfUpdate)?;
2018-12-23 13:17:53 +02:00
let result = Update::configure()
2018-12-31 22:23:04 +02:00
.and_then(|mut u| {
u.repo_owner("r-darwish")
.repo_name("topgrade")
.target(&target)
.bin_name(if cfg!(windows) { "topgrade.exe" } else { "topgrade" })
.show_output(false)
.show_download_progress(true)
.current_version(self_update_crate::cargo_crate_version!())
.no_confirm(true)
.build()
})
2019-04-14 11:35:18 +03:00
.and_then(Update::update_extended)
2018-12-11 16:43:26 +02:00
.context(ErrorKind::SelfUpdate)?;
2018-11-26 14:27:19 +02:00
2018-12-23 13:17:53 +02:00
if let GitHubUpdateStatus::Updated(release) = &result {
println!("\nTopgrade upgraded to {}:\n", release.version());
println!("{}", release.body);
2018-11-26 14:27:19 +02:00
} else {
println!("Topgrade is up-to-date");
}
#[cfg(unix)]
{
if result.updated() {
2018-12-05 11:34:08 +02:00
print_warning("Respawning...");
2018-12-11 16:43:26 +02:00
let err = Command::new(current_exe.context(ErrorKind::SelfUpdate)?)
.args(env::args().skip(1))
.env("TOPGRADE_NO_SELF_UPGRADE", "")
2018-12-11 16:43:26 +02:00
.exec();
Err(err).context(ErrorKind::SelfUpdate)?
2018-11-26 14:27:19 +02:00
}
}
Ok(())
}