Ctrl+C handling (#75)
As stated [here](https://doc.rust-lang.org/std/io/trait.BufRead.html#errors-1), `read_until` (and `read_line`) ignore Ctrl+C, so Topgrade does not respond to Ctrl+C in the retry prompt, and instead will exit only when enter is pressed after Ctrl+C. This is undesirable, so this pull request is a WIP until we find a solution.
This commit is contained in:
committed by
GitHub
parent
41621bd08f
commit
a6b6b7aa4e
176
src/main.rs
176
src/main.rs
@@ -12,7 +12,13 @@ extern crate serde;
|
||||
extern crate shellexpand;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate console;
|
||||
extern crate env_logger;
|
||||
#[cfg(unix)]
|
||||
extern crate nix;
|
||||
#[cfg(unix)]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate term_size;
|
||||
extern crate termcolor;
|
||||
extern crate walkdir;
|
||||
@@ -29,6 +35,7 @@ mod unix;
|
||||
mod windows;
|
||||
|
||||
mod config;
|
||||
mod ctrlc;
|
||||
mod executor;
|
||||
mod generic;
|
||||
mod git;
|
||||
@@ -45,6 +52,7 @@ use self::terminal::Terminal;
|
||||
use failure::Error;
|
||||
use std::borrow::Cow;
|
||||
use std::env;
|
||||
use std::io::ErrorKind;
|
||||
use std::process::exit;
|
||||
use structopt::StructOpt;
|
||||
|
||||
@@ -56,24 +64,48 @@ struct StepFailed;
|
||||
#[fail(display = "Cannot find the user base directories")]
|
||||
struct NoBaseDirectories;
|
||||
|
||||
fn execute<'a, F, M>(func: F, terminal: &mut Terminal) -> Option<(M, bool)>
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "Process Interrupted")]
|
||||
pub struct Interrupted;
|
||||
|
||||
struct ExecutionContext {
|
||||
terminal: Terminal,
|
||||
}
|
||||
|
||||
fn execute<'a, F, M>(func: F, execution_context: &mut ExecutionContext) -> Result<Option<(M, bool)>, Error>
|
||||
where
|
||||
M: Into<Cow<'a, str>>,
|
||||
F: Fn(&mut Terminal) -> Option<(M, bool)>,
|
||||
{
|
||||
while let Some((key, success)) = func(terminal) {
|
||||
while let Some((key, success)) = func(&mut execution_context.terminal) {
|
||||
if success {
|
||||
return Some((key, success));
|
||||
return Ok(Some((key, success)));
|
||||
}
|
||||
|
||||
if !terminal.should_retry() {
|
||||
return Some((key, success));
|
||||
let running = ctrlc::running();
|
||||
if !running {
|
||||
ctrlc::set_running(true);
|
||||
}
|
||||
|
||||
let should_retry = execution_context.terminal.should_retry(running).map_err(|e| {
|
||||
if e.kind() == ErrorKind::Interrupted {
|
||||
Error::from(Interrupted)
|
||||
} else {
|
||||
Error::from(e)
|
||||
}
|
||||
})?;
|
||||
|
||||
if !should_retry {
|
||||
return Ok(Some((key, success)));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn run() -> Result<(), Error> {
|
||||
ctrlc::set_handler();
|
||||
|
||||
let opt = config::Opt::from_args();
|
||||
|
||||
if opt.run_in_tmux && env::var("TMUX").is_err() {
|
||||
@@ -87,7 +119,11 @@ fn run() -> Result<(), Error> {
|
||||
let base_dirs = directories::BaseDirs::new().ok_or(NoBaseDirectories)?;
|
||||
let git = Git::new();
|
||||
let mut git_repos = Repositories::new(&git);
|
||||
let mut terminal = Terminal::new();
|
||||
|
||||
let mut execution_context = ExecutionContext {
|
||||
terminal: Terminal::new(),
|
||||
};
|
||||
|
||||
let config = Config::read(&base_dirs)?;
|
||||
let mut report = Report::new();
|
||||
|
||||
@@ -96,7 +132,7 @@ fn run() -> Result<(), Error> {
|
||||
|
||||
if let Some(commands) = config.pre_commands() {
|
||||
for (name, command) in commands {
|
||||
generic::run_custom_command(&name, &command, &mut terminal, opt.dry_run)?;
|
||||
generic::run_custom_command(&name, &command, &mut execution_context.terminal, opt.dry_run)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,8 +142,8 @@ fn run() -> Result<(), Error> {
|
||||
#[cfg(windows)]
|
||||
report.push_result(execute(
|
||||
|terminal| powershell.update_modules(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
let distribution = linux::Distribution::detect();
|
||||
@@ -119,8 +155,8 @@ fn run() -> Result<(), Error> {
|
||||
Ok(distribution) => {
|
||||
report.push_result(execute(
|
||||
|terminal| distribution.upgrade(&sudo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error detecting current distribution: {}", e);
|
||||
@@ -128,22 +164,22 @@ fn run() -> Result<(), Error> {
|
||||
}
|
||||
report.push_result(execute(
|
||||
|terminal| linux::run_etc_update(&sudo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
report.push_result(execute(
|
||||
|terminal| windows::run_chocolatey(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
#[cfg(unix)]
|
||||
report.push_result(execute(
|
||||
|terminal| unix::run_homebrew(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
if !opt.no_emacs {
|
||||
git_repos.insert(base_dirs.home_dir().join(".emacs.d"));
|
||||
@@ -178,66 +214,66 @@ fn run() -> Result<(), Error> {
|
||||
for repo in git_repos.repositories() {
|
||||
report.push_result(execute(
|
||||
|terminal| git.pull(&repo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
report.push_result(execute(
|
||||
|terminal| unix::run_zplug(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| unix::run_fisher(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| tmux::run_tpm(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_rustup(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_cargo_update(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
if !opt.no_emacs {
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_emacs(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_opam_update(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| vim::upgrade_vim(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| vim::upgrade_neovim(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| node::run_npm_upgrade(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_composer_update(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| node::yarn_global_update(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
#[cfg(
|
||||
not(
|
||||
@@ -251,27 +287,27 @@ fn run() -> Result<(), Error> {
|
||||
)]
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_apm(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| generic::run_gem(&base_dirs, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
report.push_result(execute(
|
||||
|terminal| linux::flatpak_user_update(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| linux::flatpak_global_update(&sudo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| linux::run_snap(&sudo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
|
||||
if let Some(commands) = config.commands() {
|
||||
@@ -283,8 +319,8 @@ fn run() -> Result<(), Error> {
|
||||
generic::run_custom_command(&name, &command, terminal, opt.dry_run).is_ok(),
|
||||
))
|
||||
},
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,12 +328,12 @@ fn run() -> Result<(), Error> {
|
||||
{
|
||||
report.push_result(execute(
|
||||
|terminal| linux::run_fwupdmgr(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
report.push_result(execute(
|
||||
|terminal| linux::run_needrestart(&sudo, terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -305,8 +341,8 @@ fn run() -> Result<(), Error> {
|
||||
if !opt.no_system {
|
||||
report.push_result(execute(
|
||||
|terminal| macos::upgrade_macos(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,16 +351,16 @@ fn run() -> Result<(), Error> {
|
||||
if !opt.no_system {
|
||||
report.push_result(execute(
|
||||
|terminal| powershell.windows_update(terminal, opt.dry_run),
|
||||
&mut terminal,
|
||||
));
|
||||
&mut execution_context,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
if !report.data().is_empty() {
|
||||
terminal.print_separator("Summary");
|
||||
execution_context.terminal.print_separator("Summary");
|
||||
|
||||
for (key, succeeded) in report.data() {
|
||||
terminal.print_result(key, *succeeded);
|
||||
execution_context.terminal.print_result(key, *succeeded);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -348,10 +384,14 @@ fn main() {
|
||||
exit(0);
|
||||
}
|
||||
Err(error) => {
|
||||
match error.downcast::<StepFailed>() {
|
||||
match error
|
||||
.downcast::<StepFailed>()
|
||||
.map(|_| ())
|
||||
.or_else(|error| error.downcast::<Interrupted>().map(|_| ()))
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(error) => println!("ERROR: {}", error),
|
||||
};
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user