You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
947 B

  1. use crate::builtins::{Builtin, BuiltinConfig};
  2. use crate::error::ShellError;
  3. use std::io;
  4. use system_shutdown::logout;
  5. pub struct Logout;
  6. impl Builtin for Logout {
  7. fn execute(&mut self, _: &mut BuiltinConfig, _: Vec<String>) -> Result<(), ShellError> {
  8. let mut input_confirmation = String::new();
  9. println!("Are you sure? Unsaved data will be lost.\ny/n");
  10. match io::stdin().read_line(&mut input_confirmation) {
  11. Ok(_) => {}
  12. Err(e) => {
  13. return Err(ShellError::ExecuteFailure(format!(
  14. "Couldn't read from stdin. {e}",
  15. )));
  16. }
  17. }
  18. if input_confirmation.starts_with('y') {
  19. match logout() {
  20. Ok(_) => println!("Logging out"),
  21. Err(error) => eprintln!("Failed to log out: {error}"),
  22. }
  23. } else {
  24. println!("Aborting");
  25. }
  26. Ok(())
  27. }
  28. }