Add reboot and shell ability in pause

This commit is contained in:
Roey Darwish Dror
2019-06-13 22:05:18 +03:00
parent 79febd78b5
commit a834a79f45
5 changed files with 46 additions and 8 deletions

View File

@@ -499,8 +499,16 @@ fn run() -> Result<(), Error> {
} }
if env::var("TOPGRADE_KEEP_END").is_ok() { if env::var("TOPGRADE_KEEP_END").is_ok() {
println!("\nPress any key to continue"); print_info("\nPress R to reboot, S for shell or any other key to continue");
pause(); match get_char() {
's' | 'S' => {
run_shell();
}
'r' | 'R' => {
reboot();
}
_ => (),
}
} }
if report.data().iter().all(|(_, succeeded)| *succeeded) { if report.data().iter().all(|(_, succeeded)| *succeeded) {

View File

@@ -8,3 +8,9 @@ pub mod macos;
pub mod unix; pub mod unix;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub mod windows; pub mod windows;
#[cfg(windows)]
pub use windows::reboot;
#[cfg(unix)]
pub use unix::reboot;

View File

@@ -129,3 +129,7 @@ pub fn run_sdkman(base_dirs: &BaseDirs, cleanup: bool, run_type: RunType) -> Res
Ok(()) Ok(())
} }
pub fn reboot() {
Command::new("sudo").arg("reboot").spawn().ok();
}

View File

@@ -95,3 +95,7 @@ pub fn run_wsl_topgrade(run_type: RunType) -> Result<(), Error> {
.arg(format!("TOPGRADE_PREFIX=WSL exec {}", topgrade)) .arg(format!("TOPGRADE_PREFIX=WSL exec {}", topgrade))
.check_run() .check_run()
} }
pub fn reboot() {
Command::new("shutdown").args(&["/R", "/T", "0"]).spawn().ok();
}

View File

@@ -21,6 +21,10 @@ fn shell() -> &'static str {
"powershell" "powershell"
} }
pub fn run_shell() {
Command::new(shell()).spawn().unwrap().wait().unwrap();
}
struct Terminal { struct Terminal {
width: Option<u16>, width: Option<u16>,
prefix: String, prefix: String,
@@ -84,6 +88,14 @@ impl Terminal {
.ok(); .ok();
} }
#[allow(dead_code)]
fn print_info<P: AsRef<str>>(&mut self, message: P) {
let message = message.as_ref();
self.term
.write_fmt(format_args!("{}\n", style(message).yellow().bold()))
.ok();
}
fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) { fn print_result<P: AsRef<str>>(&mut self, key: P, succeeded: bool) {
let key = key.as_ref(); let key = key.as_ref();
@@ -127,7 +139,7 @@ impl Terminal {
'y' | 'Y' => break Ok(true), 'y' | 'Y' => break Ok(true),
's' | 'S' => { 's' | 'S' => {
println!("\n\nDropping you to shell. Fix what you need and then exit the shell.\n"); 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); break Ok(true);
} }
'n' | 'N' | '\r' | '\n' => break Ok(false), 'n' | 'N' | '\r' | '\n' => break Ok(false),
@@ -140,9 +152,8 @@ impl Terminal {
answer answer
} }
fn pause(&self) -> Result<(), io::Error> { fn get_char(&self) -> Result<char, io::Error> {
self.term.read_char()?; self.term.read_char()
Ok(())
} }
} }
@@ -165,6 +176,11 @@ pub fn print_warning<P: AsRef<str>>(message: P) {
TERMINAL.lock().unwrap().print_warning(message) TERMINAL.lock().unwrap().print_warning(message)
} }
#[allow(dead_code)]
pub fn print_info<P: AsRef<str>>(message: P) {
TERMINAL.lock().unwrap().print_info(message)
}
pub fn print_result<P: AsRef<str>>(key: P, succeeded: bool) { pub fn print_result<P: AsRef<str>>(key: P, succeeded: bool) {
TERMINAL.lock().unwrap().print_result(key, succeeded) TERMINAL.lock().unwrap().print_result(key, succeeded)
} }
@@ -175,6 +191,6 @@ pub fn is_dumb() -> bool {
TERMINAL.lock().unwrap().width.is_none() TERMINAL.lock().unwrap().width.is_none()
} }
pub fn pause() { pub fn get_char() -> char {
TERMINAL.lock().unwrap().pause().unwrap(); TERMINAL.lock().unwrap().get_char().unwrap()
} }