diff --git a/src/main.rs b/src/main.rs index 42a2df80..9b4e26d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -499,8 +499,16 @@ fn run() -> Result<(), Error> { } if env::var("TOPGRADE_KEEP_END").is_ok() { - println!("\nPress any key to continue"); - pause(); + print_info("\nPress R to reboot, S for shell or any other key to continue"); + match get_char() { + 's' | 'S' => { + run_shell(); + } + 'r' | 'R' => { + reboot(); + } + _ => (), + } } if report.data().iter().all(|(_, succeeded)| *succeeded) { diff --git a/src/steps/os/mod.rs b/src/steps/os/mod.rs index 70d59b1c..c39c3dc1 100644 --- a/src/steps/os/mod.rs +++ b/src/steps/os/mod.rs @@ -8,3 +8,9 @@ pub mod macos; pub mod unix; #[cfg(target_os = "windows")] pub mod windows; + +#[cfg(windows)] +pub use windows::reboot; + +#[cfg(unix)] +pub use unix::reboot; diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 7ae0d975..3a771ea8 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -129,3 +129,7 @@ pub fn run_sdkman(base_dirs: &BaseDirs, cleanup: bool, run_type: RunType) -> Res Ok(()) } + +pub fn reboot() { + Command::new("sudo").arg("reboot").spawn().ok(); +} diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index 89a2ffe5..fdf2074f 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -95,3 +95,7 @@ pub fn run_wsl_topgrade(run_type: RunType) -> Result<(), Error> { .arg(format!("TOPGRADE_PREFIX=WSL exec {}", topgrade)) .check_run() } + +pub fn reboot() { + Command::new("shutdown").args(&["/R", "/T", "0"]).spawn().ok(); +} diff --git a/src/terminal.rs b/src/terminal.rs index 2aa0927d..74f96205 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -21,6 +21,10 @@ fn shell() -> &'static str { "powershell" } +pub fn run_shell() { + Command::new(shell()).spawn().unwrap().wait().unwrap(); +} + struct Terminal { width: Option, prefix: String, @@ -84,6 +88,14 @@ impl Terminal { .ok(); } + #[allow(dead_code)] + fn print_info>(&mut self, message: P) { + let message = message.as_ref(); + self.term + .write_fmt(format_args!("{}\n", style(message).yellow().bold())) + .ok(); + } + fn print_result>(&mut self, key: P, succeeded: bool) { let key = key.as_ref(); @@ -127,7 +139,7 @@ impl Terminal { 'y' | 'Y' => break Ok(true), 's' | 'S' => { println!("\n\nDropping you to shell. Fix what you need and then exit the shell.\n"); - Command::new(shell()).spawn().unwrap().wait().unwrap(); + run_shell(); break Ok(true); } 'n' | 'N' | '\r' | '\n' => break Ok(false), @@ -140,9 +152,8 @@ impl Terminal { answer } - fn pause(&self) -> Result<(), io::Error> { - self.term.read_char()?; - Ok(()) + fn get_char(&self) -> Result { + self.term.read_char() } } @@ -165,6 +176,11 @@ pub fn print_warning>(message: P) { TERMINAL.lock().unwrap().print_warning(message) } +#[allow(dead_code)] +pub fn print_info>(message: P) { + TERMINAL.lock().unwrap().print_info(message) +} + pub fn print_result>(key: P, succeeded: bool) { TERMINAL.lock().unwrap().print_result(key, succeeded) } @@ -175,6 +191,6 @@ pub fn is_dumb() -> bool { TERMINAL.lock().unwrap().width.is_none() } -pub fn pause() { - TERMINAL.lock().unwrap().pause().unwrap(); +pub fn get_char() -> char { + TERMINAL.lock().unwrap().get_char().unwrap() }