mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-22 05:12:27 +00:00
Merge pull request #2 from lj3954/root
refactor: Reduce nesting in root module
This commit is contained in:
commit
8ba2fdd23e
|
@ -8,19 +8,21 @@ use ratatui::{
|
|||
};
|
||||
use std::io;
|
||||
|
||||
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
|
||||
if nix::unistd::geteuid().is_root() {
|
||||
terminal.draw(|frame| {
|
||||
let root_warn = Paragraph::new(
|
||||
r#"
|
||||
const ROOT_WARNING: &str = r#"
|
||||
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
|
||||
This utility prioritizes compatibility with non-root environments.
|
||||
Some scripts may work without any issues, some may not.
|
||||
You have been warned!
|
||||
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
|
||||
Press [y] to continue, [n] to abort
|
||||
"#,
|
||||
)
|
||||
"#;
|
||||
|
||||
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
|
||||
if !nix::unistd::geteuid().is_root() {
|
||||
return Ok(true);
|
||||
}
|
||||
terminal.draw(|frame| {
|
||||
let root_warn = Paragraph::new(ROOT_WARNING)
|
||||
.white()
|
||||
.on_black()
|
||||
.alignment(Alignment::Center)
|
||||
|
@ -40,30 +42,16 @@ Press [y] to continue, [n] to abort
|
|||
})?;
|
||||
|
||||
loop {
|
||||
match event::read()? {
|
||||
Event::Key(
|
||||
KeyEvent {
|
||||
code: KeyCode::Char('y'),
|
||||
if let Event::Key(KeyEvent {
|
||||
code: KeyCode::Char(ch),
|
||||
..
|
||||
}
|
||||
| KeyEvent {
|
||||
code: KeyCode::Char('Y'),
|
||||
..
|
||||
},
|
||||
) => break,
|
||||
Event::Key(
|
||||
KeyEvent {
|
||||
code: KeyCode::Char('n'),
|
||||
..
|
||||
}
|
||||
| KeyEvent {
|
||||
code: KeyCode::Char('N'),
|
||||
..
|
||||
},
|
||||
) => return Ok(false),
|
||||
}) = event::read()?
|
||||
{
|
||||
match ch.to_ascii_lowercase() {
|
||||
'y' => return Ok(true),
|
||||
'n' => return Ok(false),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user