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:
Roey Darwish Dror
2018-10-17 14:07:58 +03:00
committed by GitHub
parent 41621bd08f
commit a6b6b7aa4e
7 changed files with 385 additions and 81 deletions

9
src/ctrlc/mod.rs Normal file
View File

@@ -0,0 +1,9 @@
#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use self::unix::*;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use self::windows::*;

29
src/ctrlc/unix.rs Normal file
View File

@@ -0,0 +1,29 @@
use nix::sys::signal;
use std::sync::atomic::{AtomicBool, Ordering};
lazy_static! {
static ref RUNNING: AtomicBool = AtomicBool::new(true);
}
pub fn running() -> bool {
RUNNING.load(Ordering::SeqCst)
}
pub fn set_running(value: bool) {
RUNNING.store(value, Ordering::SeqCst)
}
extern "C" fn handle_sigint(_: i32) {
set_running(false);
}
pub fn set_handler() {
let sig_action = signal::SigAction::new(
signal::SigHandler::Handler(handle_sigint),
signal::SaFlags::empty(),
signal::SigSet::empty(),
);
unsafe {
signal::sigaction(signal::SIGINT, &sig_action).unwrap();
}
}

7
src/ctrlc/windows.rs Normal file
View File

@@ -0,0 +1,7 @@
pub fn running() -> bool {
true
}
pub fn set_running(_value: bool) {}
pub fn set_handler() {}

View File

@@ -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);
}
}

View File

@@ -1,5 +1,6 @@
use console::Term;
use std::cmp::{max, min};
use std::io::{stdin, Write};
use std::io::{self, Write};
use term_size;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
@@ -67,29 +68,27 @@ impl Terminal {
let _ = self.stdout.flush();
}
pub fn should_retry(&mut self) -> bool {
pub fn should_retry(&mut self, running: bool) -> Result<bool, io::Error> {
if self.width.is_none() {
return false;
return Ok(false);
}
println!();
loop {
let mut result = String::new();
let _ = self
.stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true));
let _ = write!(&mut self.stdout, "Retry? [y/N] ");
if !running {
write!(&mut self.stdout, "(Press Ctrl+C again to stop Topgrade) ");
}
let _ = self.stdout.reset();
let _ = self.stdout.flush();
if stdin().read_line(&mut result).is_ok() {
match result.as_str().trim() {
"y" | "Y" => return true,
"n" | "N" | "" => return false,
_ => (),
}
} else {
return false;
match Term::stdout().read_char()? {
'y' | 'Y' => return Ok(true),
'n' | 'N' | '\n' => return Ok(false),
_ => (),
}
}
}