diff --git a/CHANGELOG.md b/CHANGELOG.md index db1e752..842703e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Changelog for binary "usermgmt" and library "usermgmt_lib". ### Added +- Added CLI option to specify the configuration file - Logging also performed to logging file - Ssh key pair can be provided by field within configuration file or as a CLI argument. diff --git a/usermgmt/src/main.rs b/usermgmt/src/main.rs index e754505..c2b1bf7 100644 --- a/usermgmt/src/main.rs +++ b/usermgmt/src/main.rs @@ -6,7 +6,7 @@ use cli_ssh_credential::CliSshCredential; use ldap_cli_credential::LdapCliCredential; use log::error; use std::process::ExitCode; -use usermgmt_lib::cli::{self, Commands, GeneralArgs, OnWhichSystem}; +use usermgmt_lib::cli::{Commands, GeneralArgs, OnWhichSystem}; use usermgmt_lib::config::{self}; use usermgmt_lib::{operations, prelude::*, ChangesToUser, Entity}; @@ -43,7 +43,7 @@ fn execute_command() -> AppResult { /// - If the LDAP or SSH session could not be established because of connection problems or invalid /// credentials. /// - If some arguments in CLI, parameter `args`, for action are not valid. -pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { +pub fn run_mgmt(args: GeneralArgs) -> AppResult { let ldap_credential = LdapCliCredential::default(); match args.command { Commands::GenerateConfig => { @@ -54,7 +54,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { to_add, on_which_sys, } => { - let config = config::load_config(None)?.config; + let config = config::load_config(args.config_file)?.config; let on_which_sys = &OnWhichSystem::from_config_for_all(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::add_user( @@ -66,7 +66,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { )? } Commands::Modify { data, on_which_sys } => { - let config = config::load_config(None)?.config; + let config = config::load_config(args.config_file)?.config; let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); let data = Entity::new_modifieble_conf(data, &config)?; @@ -80,7 +80,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { )? } Commands::Delete { user, on_which_sys } => { - let config = config::load_config(None)?.config; + let config = config::load_config(args.config_file)?.config; let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::delete_user( @@ -95,7 +95,7 @@ pub fn run_mgmt(args: cli::GeneralArgs) -> AppResult { on_which_sys, simple_output_for_ldap, } => { - let config = config::load_config(None)?.config; + let config = config::load_config(args.config_file)?.config; let on_which_sys = &OnWhichSystem::from_config_for_slurm_ldap(&config, &on_which_sys); let cli_ssh_credential = CliSshCredential::new(&config, on_which_sys.ssh_path()); operations::print_list_of_users_to_stdout( diff --git a/usermgmt_gui/Cargo.toml b/usermgmt_gui/Cargo.toml index 9ed5fc7..437d022 100644 --- a/usermgmt_gui/Cargo.toml +++ b/usermgmt_gui/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license-file = "LICENSE" description = "GUI for simultaneous Slurm and LDAP user management." readme = "README.md" -rust-version = "1.72.0" +rust-version = "1.76.0" [dependencies] usermgmt_lib = { path = "../usermgmt_lib" } diff --git a/usermgmt_gui/src/drawing/draw_add_state.rs b/usermgmt_gui/src/drawing/draw_add_state.rs index bba2c60..8c7b4c0 100644 --- a/usermgmt_gui/src/drawing/draw_add_state.rs +++ b/usermgmt_gui/src/drawing/draw_add_state.rs @@ -91,7 +91,10 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) { }); fn request_addition_of_user(window: &mut UsermgmtWindow) -> AppResult { - window.adding_state.last_added_username = window.adding_state.username.clone(); + window + .adding_state + .last_added_username + .clone_from(&window.adding_state.username); if let Ok(prep) = general_utils::prep_conf_creds(window, |app| &mut app.adding_state.adding_res_io, true) { diff --git a/usermgmt_gui/src/drawing/draw_delete_state.rs b/usermgmt_gui/src/drawing/draw_delete_state.rs index 2f474a1..8d4efad 100644 --- a/usermgmt_gui/src/drawing/draw_delete_state.rs +++ b/usermgmt_gui/src/drawing/draw_delete_state.rs @@ -42,7 +42,10 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) { } fn delete_user(window: &mut UsermgmtWindow) { - window.remove_state.last_username = window.remove_state.username.clone(); + window + .remove_state + .last_username + .clone_from(&window.remove_state.username); if let Ok(prep) = general_utils::prep_conf_creds(window, |app| &mut app.remove_state.remove_res_io, false) { diff --git a/usermgmt_gui/src/drawing/modify_state.rs b/usermgmt_gui/src/drawing/modify_state.rs index c184018..eb96d6a 100644 --- a/usermgmt_gui/src/drawing/modify_state.rs +++ b/usermgmt_gui/src/drawing/modify_state.rs @@ -29,7 +29,10 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) { } fn handle_modify_req(window: &mut UsermgmtWindow) { - window.modify_state.last_added_username = window.modify_state.username.clone(); + window + .modify_state + .last_added_username + .clone_from(&window.modify_state.username); if let Ok(PreparationBeforeIoTask { ldap_cred, ssh_cred, diff --git a/usermgmt_gui/src/main_logic/query_io_tasks.rs b/usermgmt_gui/src/main_logic/query_io_tasks.rs index 9dd96b1..01b756d 100644 --- a/usermgmt_gui/src/main_logic/query_io_tasks.rs +++ b/usermgmt_gui/src/main_logic/query_io_tasks.rs @@ -7,7 +7,7 @@ pub fn query(window: &mut UsermgmtWindow) { let listing_state = &mut window.listin_state; let ssh_state = &mut window.ssh_state; let path = &mut window.conf_path; - *path = conf.path.to_owned(); + path.clone_from(&conf.path); let config = &conf.config; if listing_state.rw_user_name.is_none() { if let Some(rw_user) = config.ldap_readonly_user.as_deref() { diff --git a/usermgmt_lib/src/cli.rs b/usermgmt_lib/src/cli.rs index 5844806..3b38b31 100644 --- a/usermgmt_lib/src/cli.rs +++ b/usermgmt_lib/src/cli.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + pub use on_which_system::{OnSlurmLdapOnlyCli, OnWhichSystem, OnWhichSystemCli, OptFilePath}; mod on_which_system; @@ -35,6 +37,11 @@ pub struct GeneralArgs { /// Operation to conduct on the user. Either add, delete or modify. #[clap(subcommand)] pub command: Commands, + #[arg(long)] + /// Allows to specify a configuration file by providing a file path. + /// If absent, the configuration file is searched under certain places like the app config + /// folder. + pub config_file: Option, } #[derive(Subcommand, Debug)] diff --git a/usermgmt_lib/src/config/path_sources.rs b/usermgmt_lib/src/config/path_sources.rs index e6b5a03..5de5966 100644 --- a/usermgmt_lib/src/config/path_sources.rs +++ b/usermgmt_lib/src/config/path_sources.rs @@ -134,7 +134,18 @@ fn may_return_path_to_conf( try_exists: impl Fn(&Path) -> io::Result, path: &Path, ) -> Option { - let to_check = path.join(constants::NAME_CONFIG_FILE); + let to_check = if path.is_file() { + debug!("Path at {:?} is dected as a configuration file.", path); + path.to_path_buf() + } else { + debug!( + "Path at {0:?} is dected as a directory.\n\ + A configuration file named {1} is search within this directory.", + path, + constants::NAME_CONFIG_FILE + ); + path.join(constants::NAME_CONFIG_FILE) + }; match try_exists(&to_check) { Ok(exits) => { if exits {