Skip to content

Commit

Permalink
Seperate root check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 committed Oct 14, 2024
1 parent 1504f30 commit 3135d3a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 69 deletions.
75 changes: 6 additions & 69 deletions tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod filter;
mod float;
mod floating_text;
mod hint;
mod root;
mod running_command;
pub mod state;
mod theme;
Expand All @@ -15,18 +16,12 @@ use std::{
use crate::theme::Theme;
use clap::Parser;
use crossterm::{
event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind},
event::{self, DisableMouseCapture, Event, KeyEventKind},
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{
backend::CrosstermBackend,
layout::{Alignment, Constraint, Layout},
style::Stylize,
widgets::{Paragraph, Wrap},
Terminal,
};
use ratatui::{backend::CrosstermBackend, Terminal};
use state::AppState;

// Linux utility toolbox
Expand Down Expand Up @@ -67,67 +62,9 @@ fn run(
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
state: &mut AppState,
) -> io::Result<()> {
if sudo::check() == sudo::RunningAs::Root {
terminal.draw(|frame| {
let root_warn = Paragraph::new(
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
"#,
)
.on_black()
.white()
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });

let rects = Layout::vertical([
Constraint::Fill(1),
Constraint::Length(10),
Constraint::Fill(1),
])
.split(frame.area());

let centered = rects[1];

frame.render_widget(root_warn, centered);
})?;

loop {
match event::read()? {
Event::Key(
KeyEvent {
code: KeyCode::Char('y'),
..
}
| KeyEvent {
code: KeyCode::Char('Y'),
..
},
) => {
break;
}
Event::Key(
KeyEvent {
code: KeyCode::Char('n'),
..
}
| KeyEvent {
code: KeyCode::Char('N'),
..
},
) => {
return Ok(());
}
_ => {}
}
}
if root::check_root(terminal)? == 1 {
println!("User aborted the program.");
return Ok(());
}

loop {
Expand Down
69 changes: 69 additions & 0 deletions tui/src/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use ratatui::{
backend::CrosstermBackend,
crossterm::event::{self, Event, KeyCode, KeyEvent},
layout::{Alignment, Constraint, Layout},
style::{Style, Stylize},
widgets::{Paragraph, Wrap},
Terminal,
};
use std::io;

pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<u8> {
if sudo::check() == sudo::RunningAs::Root {
terminal.draw(|frame| {
let root_warn = Paragraph::new(
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
"#,
)
.white()
.on_black()
.alignment(Alignment::Center)
.style(Style::default().bold())
.wrap(Wrap { trim: true });

let rects = Layout::vertical([
Constraint::Fill(1),
Constraint::Length(10),
Constraint::Fill(1),
])
.split(frame.area());

let centered = rects[1];

frame.render_widget(root_warn, centered);
})?;

loop {
match event::read()? {
Event::Key(
KeyEvent {
code: KeyCode::Char('y'),
..
}
| KeyEvent {
code: KeyCode::Char('Y'),
..
},
) => break,
Event::Key(
KeyEvent {
code: KeyCode::Char('n'),
..
}
| KeyEvent {
code: KeyCode::Char('N'),
..
},
) => return Ok(1),
_ => {}
}
}
}
Ok(0)
}

0 comments on commit 3135d3a

Please sign in to comment.