Skip to content

Commit da6a10f

Browse files
Seperate root check logic
1 parent f472ab9 commit da6a10f

File tree

4 files changed

+95
-82
lines changed

4 files changed

+95
-82
lines changed

Cargo.lock

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tui/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tree-sitter-highlight = "0.24.2"
3030
tree-sitter-bash = "0.23.1"
3131
anstyle = "1.0.8"
3232
ansi-to-tui = "6.0.0"
33-
sudo = "0.6.0"
3433
zips = "0.1.7"
34+
nix = { version = "0.29.0", features = [ "user" ] }
3535

3636
[build-dependencies]
3737
chrono = "0.4.33"

tui/src/main.rs

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod filter;
33
mod float;
44
mod floating_text;
55
mod hint;
6+
mod root;
67
mod running_command;
78
pub mod state;
89
mod theme;
@@ -15,18 +16,12 @@ use std::{
1516
use crate::theme::Theme;
1617
use clap::Parser;
1718
use crossterm::{
18-
event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind},
19+
event::{self, DisableMouseCapture, Event, KeyEventKind},
1920
style::ResetColor,
2021
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
2122
ExecutableCommand,
2223
};
23-
use ratatui::{
24-
backend::CrosstermBackend,
25-
layout::{Alignment, Constraint, Layout},
26-
style::Stylize,
27-
widgets::{Paragraph, Wrap},
28-
Terminal,
29-
};
24+
use ratatui::{backend::CrosstermBackend, Terminal};
3025
use state::AppState;
3126

3227
// Linux utility toolbox
@@ -73,67 +68,8 @@ fn run(
7368
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
7469
state: &mut AppState,
7570
) -> io::Result<()> {
76-
if sudo::check() == sudo::RunningAs::Root {
77-
terminal.draw(|frame| {
78-
let root_warn = Paragraph::new(
79-
r#"
80-
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
81-
82-
This utility prioritizes compatibility with non-root environments.
83-
Some scripts may work without any issues, some may not.
84-
You have been warned!
85-
86-
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
87-
88-
Press [y] to continue, [n] to abort
89-
"#,
90-
)
91-
.on_black()
92-
.white()
93-
.alignment(Alignment::Center)
94-
.wrap(Wrap { trim: true });
95-
96-
let rects = Layout::vertical([
97-
Constraint::Fill(1),
98-
Constraint::Length(10),
99-
Constraint::Fill(1),
100-
])
101-
.split(frame.area());
102-
103-
let centered = rects[1];
104-
105-
frame.render_widget(root_warn, centered);
106-
})?;
107-
108-
loop {
109-
match event::read()? {
110-
Event::Key(
111-
KeyEvent {
112-
code: KeyCode::Char('y'),
113-
..
114-
}
115-
| KeyEvent {
116-
code: KeyCode::Char('Y'),
117-
..
118-
},
119-
) => {
120-
break;
121-
}
122-
Event::Key(
123-
KeyEvent {
124-
code: KeyCode::Char('n'),
125-
..
126-
}
127-
| KeyEvent {
128-
code: KeyCode::Char('N'),
129-
..
130-
},
131-
) => {
132-
return Ok(());
133-
}
134-
_ => {}
135-
}
136-
}
71+
if !root::check_root(terminal)? {
72+
return Ok(());
13773
}
13874

13975
loop {

tui/src/root.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use ratatui::{
2+
backend::CrosstermBackend,
3+
crossterm::event::{self, Event, KeyCode, KeyEvent},
4+
layout::{Alignment, Constraint, Layout},
5+
style::{Style, Stylize},
6+
widgets::{Paragraph, Wrap},
7+
Terminal,
8+
};
9+
use std::io;
10+
11+
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
12+
if nix::unistd::geteuid().is_root() {
13+
terminal.draw(|frame| {
14+
let root_warn = Paragraph::new(
15+
r#"
16+
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
17+
This utility prioritizes compatibility with non-root environments.
18+
Some scripts may work without any issues, some may not.
19+
You have been warned!
20+
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
21+
Press [y] to continue, [n] to abort
22+
"#,
23+
)
24+
.white()
25+
.on_black()
26+
.alignment(Alignment::Center)
27+
.style(Style::default().bold())
28+
.wrap(Wrap { trim: true });
29+
30+
let rects = Layout::vertical([
31+
Constraint::Fill(1),
32+
Constraint::Length(10),
33+
Constraint::Fill(1),
34+
])
35+
.split(frame.area());
36+
37+
let centered = rects[1];
38+
39+
frame.render_widget(root_warn, centered);
40+
})?;
41+
42+
loop {
43+
match event::read()? {
44+
Event::Key(
45+
KeyEvent {
46+
code: KeyCode::Char('y'),
47+
..
48+
}
49+
| KeyEvent {
50+
code: KeyCode::Char('Y'),
51+
..
52+
},
53+
) => break,
54+
Event::Key(
55+
KeyEvent {
56+
code: KeyCode::Char('n'),
57+
..
58+
}
59+
| KeyEvent {
60+
code: KeyCode::Char('N'),
61+
..
62+
},
63+
) => return Ok(false),
64+
_ => {}
65+
}
66+
}
67+
}
68+
Ok(true)
69+
}

0 commit comments

Comments
 (0)