Skip to content

Commit

Permalink
Merge pull request #1 from cartercanedy/root-user-warning
Browse files Browse the repository at this point in the history
implement root warning prompt
  • Loading branch information
jeevithakannan2 authored Oct 14, 2024
2 parents 99b1f7a + ff02858 commit 12edc43
Showing 1 changed file with 71 additions and 8 deletions.
79 changes: 71 additions & 8 deletions tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ use std::{
use crate::theme::Theme;
use clap::Parser;
use crossterm::{
event::{self, DisableMouseCapture, Event, KeyEventKind},
event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind},
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{backend::CrosstermBackend, Terminal};
use ratatui::{
backend::CrosstermBackend,
layout::{Alignment, Constraint, Layout},
style::Stylize,
widgets::{Paragraph, Wrap},
Terminal,
};
use state::AppState;

// Linux utility toolbox
Expand All @@ -39,12 +45,6 @@ struct Args {
}

fn main() -> io::Result<()> {
if sudo::check() != sudo::RunningAs::User && !Args::parse().allow_root {
eprintln!("Error: This program is not intended to be run with elevated privileges.");
eprintln!("To bypass this restriction, use the '--allow-root' flag.");
std::process::exit(1);
}

let args = Args::parse();

let mut state = AppState::new(args.theme, args.override_validation);
Expand All @@ -70,6 +70,69 @@ 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(());
}
_ => {}
}
}
}

loop {
terminal.draw(|frame| state.draw(frame)).unwrap();
// Wait for an event
Expand Down

0 comments on commit 12edc43

Please sign in to comment.