implement a root check menu

This commit is contained in:
nnyyxxxx 2024-11-08 01:53:12 -05:00
parent f5f38243f0
commit eab47e9eb8
No known key found for this signature in database
GPG Key ID: 6038FFD6589902CB
5 changed files with 74 additions and 2 deletions

21
Cargo.lock generated
View File

@ -157,6 +157,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.20" version = "4.5.20"
@ -436,6 +442,7 @@ dependencies = [
"anstyle", "anstyle",
"clap", "clap",
"linutil_core", "linutil_core",
"nix 0.29.0",
"oneshot", "oneshot",
"portable-pty", "portable-pty",
"rand", "rand",
@ -528,6 +535,18 @@ dependencies = [
"pin-utils", "pin-utils",
] ]
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@ -598,7 +617,7 @@ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
"log", "log",
"nix", "nix 0.25.1",
"serial", "serial",
"shared_library", "shared_library",
"shell-words", "shell-words",

View File

@ -29,6 +29,7 @@ textwrap = "0.16.1"
anstyle = "1.0.8" anstyle = "1.0.8"
ansi-to-tui = "7.0.0" ansi-to-tui = "7.0.0"
zips = "0.1.7" zips = "0.1.7"
nix = { version = "0.29.0", features = [ "user" ] }
[[bin]] [[bin]]
name = "linutil" name = "linutil"

View File

@ -3,6 +3,7 @@ mod filter;
mod float; mod float;
mod floating_text; mod floating_text;
mod hint; mod hint;
mod root;
mod running_command; mod running_command;
pub mod state; pub mod state;
mod theme; mod theme;
@ -28,6 +29,8 @@ use ratatui::{
}; };
use state::AppState; use state::AppState;
use crate::root::create_app_state;
// Linux utility toolbox // Linux utility toolbox
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Args { struct Args {
@ -54,7 +57,7 @@ struct Args {
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let mut state = AppState::new( let mut state = create_app_state(
args.config, args.config,
args.theme, args.theme,
args.override_validation, args.override_validation,

45
tui/src/root.rs Normal file
View File

@ -0,0 +1,45 @@
use crate::{
float::Float, float::FloatContent, floating_text::FloatingText, state::AppState, theme::Theme,
};
use std::path::PathBuf;
#[cfg(unix)]
use nix::unistd::Uid;
pub fn create_app_state(
config_path: Option<PathBuf>,
theme: Theme,
override_validation: bool,
size_bypass: bool,
skip_confirmation: bool,
) -> AppState {
#[cfg(unix)]
{
if Uid::effective().is_root() {
let mut state = AppState::new(
config_path,
theme,
override_validation,
size_bypass,
skip_confirmation,
);
let warning = FloatingText::new(
"WARNING: You are running this utility as root!\n\nThis means you have full system access and commands can potentially damage your system if used incorrectly.\n\nPlease proceed with caution and make sure you understand what each command does before executing it.".into(),
"Root User Warning",
true,
);
let float: Float<dyn FloatContent> = Float::new(Box::new(warning), 60, 40);
state.set_float_window(float);
return state;
}
}
AppState::new(
config_path,
theme,
override_validation,
size_bypass,
skip_confirmation,
)
}

View File

@ -873,6 +873,10 @@ impl AppState {
self.current_tab.select(Some(next)); self.current_tab.select(Some(next));
self.refresh_tab(); self.refresh_tab();
} }
pub fn set_float_window(&mut self, float: Float<dyn FloatContent + 'static>) {
self.focus = Focus::FloatingWindow(float);
}
} }
#[cfg(feature = "tips")] #[cfg(feature = "tips")]