2022-11-23 15:23:00 +00:00
|
|
|
use std::env;
|
|
|
|
|
#[cfg(unix)]
|
2022-11-08 05:54:35 -05:00
|
|
|
use std::os::unix::process::CommandExt as _;
|
2022-11-23 15:23:00 +00:00
|
|
|
use std::process::Command;
|
2022-11-23 15:18:09 +00:00
|
|
|
|
2022-11-11 09:39:29 -05:00
|
|
|
use color_eyre::eyre::{bail, Result};
|
2022-11-08 05:54:35 -05:00
|
|
|
use self_update_crate::backends::github::Update;
|
|
|
|
|
use self_update_crate::update::UpdateStatus;
|
|
|
|
|
|
|
|
|
|
use super::terminal::*;
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
use crate::error::Upgraded;
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn self_update() -> Result<()> {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("Self update");
|
2018-11-26 14:27:19 +02:00
|
|
|
let current_exe = env::current_exe();
|
|
|
|
|
|
2019-08-13 14:27:41 +03:00
|
|
|
let target = self_update_crate::get_target();
|
2018-12-23 13:17:53 +02:00
|
|
|
let result = Update::configure()
|
2022-11-02 10:10:16 +00:00
|
|
|
.repo_owner("topgrade-rs")
|
2019-08-13 14:27:41 +03:00
|
|
|
.repo_name("topgrade")
|
2021-09-02 16:08:58 +03:00
|
|
|
.target(target)
|
2022-11-06 13:54:38 +00:00
|
|
|
.bin_name(if cfg!(windows) { "topgrade.exe" } else { "topgrade" })
|
2019-08-13 14:27:41 +03:00
|
|
|
.show_output(false)
|
|
|
|
|
.show_download_progress(true)
|
|
|
|
|
.current_version(self_update_crate::cargo_crate_version!())
|
|
|
|
|
.no_confirm(true)
|
2020-05-09 07:22:51 +03:00
|
|
|
.build()?
|
|
|
|
|
.update_extended()?;
|
2018-11-26 14:27:19 +02:00
|
|
|
|
2020-05-09 07:22:51 +03:00
|
|
|
if let UpdateStatus::Updated(release) = &result {
|
|
|
|
|
println!("\nTopgrade upgraded to {}:\n", release.version);
|
|
|
|
|
if let Some(body) = &release.body {
|
|
|
|
|
println!("{}", body);
|
|
|
|
|
}
|
2018-11-26 14:27:19 +02:00
|
|
|
} else {
|
|
|
|
|
println!("Topgrade is up-to-date");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
if result.updated() {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_warning("Respawning...");
|
2019-12-11 23:05:38 +02:00
|
|
|
let mut command = Command::new(current_exe?);
|
2019-06-03 09:41:25 +03:00
|
|
|
command.args(env::args().skip(1)).env("TOPGRADE_NO_SELF_UPGRADE", "");
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
{
|
|
|
|
|
let err = command.exec();
|
2019-12-11 23:05:38 +02:00
|
|
|
bail!(err);
|
2019-06-03 09:41:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
{
|
2022-11-08 05:54:35 -05:00
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
let status = command.status()?;
|
2019-12-11 23:05:38 +02:00
|
|
|
bail!(Upgraded(status));
|
2019-06-03 09:41:25 +03:00
|
|
|
}
|
2018-11-26 14:27:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|