From 64a89aa26818e8096b5c479833245f8bc8e86afa Mon Sep 17 00:00:00 2001 From: TheAlexDev23 Date: Thu, 24 Oct 2024 23:04:21 +0200 Subject: [PATCH] Add the ability to override the default root during setup using the cli utility #24 --- crates/power-daemon-mgr/src/main.rs | 11 +++++-- crates/power-daemon-mgr/src/setup.rs | 43 +++++++++++----------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/crates/power-daemon-mgr/src/main.rs b/crates/power-daemon-mgr/src/main.rs index 6108c1c..d75b87f 100644 --- a/crates/power-daemon-mgr/src/main.rs +++ b/crates/power-daemon-mgr/src/main.rs @@ -28,7 +28,12 @@ struct Args { #[derive(Debug, Clone, Subcommand)] enum OpMode { - Setup, + Setup { + #[arg(default_value = "/")] + /// Use this parameter if you want to override the default root + /// installation directory + root: PathBuf, + }, GenerateBaseFiles { #[arg(long)] path: PathBuf, @@ -95,13 +100,13 @@ async fn main() { log::set_max_level(args.verbose.log_level_filter()); match args.mode { - OpMode::Setup => setup(), + OpMode::Setup { root } => setup(&root), OpMode::Daemon => daemon().await, OpMode::GenerateBaseFiles { path, program_path, verbose_daemon, - } => generate_base_files(path, program_path, verbose_daemon), + } => generate_base_files(&path, &program_path, verbose_daemon), OpMode::ListProfiles => { println!( "{:?}", diff --git a/crates/power-daemon-mgr/src/setup.rs b/crates/power-daemon-mgr/src/setup.rs index 70490b7..34963ad 100644 --- a/crates/power-daemon-mgr/src/setup.rs +++ b/crates/power-daemon-mgr/src/setup.rs @@ -1,48 +1,39 @@ -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::{fs, path::Path}; -use log::{debug, error, trace}; -use nix::unistd::Uid; +use log::{debug, trace}; use power_daemon::{profiles_generator, Config, DefaultProfileType, SystemInfo}; -pub fn setup() { - if !Uid::effective().is_root() { - error!("Root priviliges required"); - return; - } - +pub fn setup(root: &Path) { let agreed = std::process::Command::new("yad").args([ "--selectable-labels", "--title", "Warning: do you want power-options to generate profiles?", "--text", - "By default, power-options will generate profiles based on the features of your system, and apply them.\nPlease refer to the wiki (https://github.com/TheAlexDev23/power-options/wiki/Default-generated-settings) to be aware of potential issues that might arise.\nIf you click cancel, an empty default profile will be generated." + "By default, power-options will generate profiles based on the features of your system, and it might apply them.\nPlease refer to the wiki (https://github.com/TheAlexDev23/power-options/wiki/Default-generated-settings) to be aware of potential issues that might arise.\nIf you click cancel, an empty default profile will be generated." ]).spawn().expect("Could not spawn popup").wait().expect("Could not wait from popup").success(); if agreed { - generate_config_files("/".into()); + generate_config_files(root); } else { - generate_empty_config_files("/".into()); + generate_empty_config_files(root); } } -pub fn generate_base_files(path: PathBuf, program_path: PathBuf, verbose_daemon: bool) { - generate_udev_file(&path, &program_path); - generate_acpi_file(&path, &program_path); - generate_dbus_file(&path); - genereate_systemd_file(&path, &program_path, verbose_daemon); +pub fn generate_base_files(path: &Path, program_path: &Path, verbose_daemon: bool) { + generate_udev_file(path, program_path); + generate_acpi_file(path, program_path); + generate_dbus_file(path); + genereate_systemd_file(path, program_path, verbose_daemon); } -fn generate_config_files(path: PathBuf) { - create_config(&path, &Config::create_default()); - generate_profiles(&path); +fn generate_config_files(path: &Path) { + create_config(path, &Config::create_default()); + generate_profiles(path); } -fn generate_empty_config_files(path: PathBuf) { - create_config(&path, &Config::create_empty()); - generate_empty_profile(&path); +fn generate_empty_config_files(path: &Path) { + create_config(path, &Config::create_empty()); + generate_empty_profile(path); } fn create_config(path: &Path, config: &Config) {