mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2025-02-22 00:55:11 +00:00
implement root check
This commit is contained in:
parent
c997a3d89e
commit
937cf1522b
|
@ -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
|
||||
|
@ -36,12 +42,6 @@ struct Args {
|
|||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
if sudo::check() != sudo::RunningAs::User {
|
||||
eprintln!("Error: This program is not intended to be run with elevated privileges.");
|
||||
eprintln!("Please run this program as a regular user to ensure proper security and functionality.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
let mut state = AppState::new(args.theme, args.override_validation);
|
||||
|
@ -67,6 +67,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
|
||||
|
|
Loading…
Reference in New Issue
Block a user