Respawn Topgrade when version is upgraded (fix #85)
This commit is contained in:
@@ -27,6 +27,8 @@ distribution which ships the latest version of Rust, such as Arch Linux.
|
|||||||
## Usage
|
## Usage
|
||||||
Just run `topgrade`. It will run the following steps:
|
Just run `topgrade`. It will run the following steps:
|
||||||
|
|
||||||
|
* Try to self-upgrade if compiled with this feature. On Unix systems Topgrade will also respawn
|
||||||
|
itself if it was upgraded
|
||||||
* *Linux*: Run the system package manager:
|
* *Linux*: Run the system package manager:
|
||||||
* *Arch*: Run [yay](https://github.com/Jguer/yay) or fall back to pacman
|
* *Arch*: Run [yay](https://github.com/Jguer/yay) or fall back to pacman
|
||||||
* *CentOS/RHEL*: Run `yum upgrade`
|
* *CentOS/RHEL*: Run `yum upgrade`
|
||||||
|
|||||||
@@ -229,40 +229,3 @@ pub fn run_composer_update(
|
|||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
#[cfg(all(
|
|
||||||
feature = "self-update",
|
|
||||||
any(windows, target_os = "linux", target_os = "macos")
|
|
||||||
))]
|
|
||||||
pub fn self_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
|
|
||||||
terminal.print_separator("Self update");
|
|
||||||
let success = if !dry_run {
|
|
||||||
let result = || -> Result<(), Error> {
|
|
||||||
let target = self_update::get_target()?;
|
|
||||||
self_update::backends::github::Update::configure()?
|
|
||||||
.repo_owner("r-darwish")
|
|
||||||
.repo_name("topgrade")
|
|
||||||
.target(&target)
|
|
||||||
.bin_name("topgrade")
|
|
||||||
.show_download_progress(true)
|
|
||||||
.current_version(cargo_crate_version!())
|
|
||||||
.no_confirm(true)
|
|
||||||
.build()?
|
|
||||||
.update()?;
|
|
||||||
Ok(())
|
|
||||||
}();
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Err(e) => {
|
|
||||||
println!("{}", e);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
Ok(_) => (true),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(("Self update", success))
|
|
||||||
}
|
|
||||||
|
|||||||
65
src/main.rs
65
src/main.rs
@@ -19,10 +19,7 @@ extern crate nix;
|
|||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
#[cfg(all(
|
#[cfg(feature = "self-update")]
|
||||||
feature = "self-update",
|
|
||||||
any(windows, target_os = "linux", target_os = "macos")
|
|
||||||
))]
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate self_update;
|
extern crate self_update;
|
||||||
extern crate walkdir;
|
extern crate walkdir;
|
||||||
@@ -59,7 +56,11 @@ use failure::Error;
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
|
#[cfg(all(unix, feature = "self-update"))]
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
#[cfg(all(unix, feature = "self-update"))]
|
||||||
|
use std::process::Command;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
#[derive(Fail, Debug)]
|
#[derive(Fail, Debug)]
|
||||||
@@ -109,6 +110,44 @@ where
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
#[cfg(feature = "self-update")]
|
||||||
|
pub fn self_update(terminal: &mut Terminal) -> Result<(), Error> {
|
||||||
|
terminal.print_separator("Self update");
|
||||||
|
#[cfg(unix)]
|
||||||
|
let current_exe = env::current_exe();
|
||||||
|
|
||||||
|
let target = self_update::get_target()?;
|
||||||
|
let result = self_update::backends::github::Update::configure()?
|
||||||
|
.repo_owner("r-darwish")
|
||||||
|
.repo_name("topgrade")
|
||||||
|
.target(&target)
|
||||||
|
.bin_name("topgrade")
|
||||||
|
.show_output(false)
|
||||||
|
.show_download_progress(true)
|
||||||
|
.current_version(cargo_crate_version!())
|
||||||
|
.no_confirm(true)
|
||||||
|
.build()?
|
||||||
|
.update()?;
|
||||||
|
|
||||||
|
if let self_update::Status::Updated(version) = &result {
|
||||||
|
println!("\nTopgrade upgraded to {}", version);
|
||||||
|
} else {
|
||||||
|
println!("Topgrade is up-to-date");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
if result.updated() {
|
||||||
|
terminal.print_warning("Respawning...");
|
||||||
|
let err = Command::new(current_exe?).args(env::args().skip(1)).exec();
|
||||||
|
Err(err)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn run() -> Result<(), Error> {
|
fn run() -> Result<(), Error> {
|
||||||
ctrlc::set_handler();
|
ctrlc::set_handler();
|
||||||
|
|
||||||
@@ -137,14 +176,16 @@ fn run() -> Result<(), Error> {
|
|||||||
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
|
#[cfg(any(target_os = "freebsd", target_os = "linux"))]
|
||||||
let sudo = utils::which("sudo");
|
let sudo = utils::which("sudo");
|
||||||
|
|
||||||
#[cfg(all(
|
#[cfg(feature = "self-update")]
|
||||||
feature = "self-update",
|
{
|
||||||
any(windows, target_os = "linux", target_os = "macos")
|
if !opt.dry_run {
|
||||||
))]
|
if let Err(e) = self_update(&mut execution_context.terminal) {
|
||||||
report.push_result(execute(
|
execution_context
|
||||||
|terminal| generic::self_update(terminal, opt.dry_run),
|
.terminal
|
||||||
&mut execution_context,
|
.print_warning(format!("Self update error: {}", e));
|
||||||
)?);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(commands) = config.pre_commands() {
|
if let Some(commands) = config.pre_commands() {
|
||||||
for (name, command) in commands {
|
for (name, command) in commands {
|
||||||
|
|||||||
@@ -89,3 +89,9 @@ impl Terminal {
|
|||||||
answer
|
answer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Terminal {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user