Skip to content

Commit

Permalink
Merge pull request #87 from BoolPurist/feat_cli_arg_conf_file
Browse files Browse the repository at this point in the history
Feat: Added CLI option to allow to specify conf file
  • Loading branch information
BoolPurist authored May 4, 2024
2 parents 1d78342 + 7b27433 commit ee54069
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 6 additions & 6 deletions usermgmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 => {
Expand All @@ -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(
Expand All @@ -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)?;
Expand All @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion usermgmt_gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 4 additions & 1 deletion usermgmt_gui/src/drawing/draw_add_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 4 additions & 1 deletion usermgmt_gui/src/drawing/draw_delete_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 4 additions & 1 deletion usermgmt_gui/src/drawing/modify_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion usermgmt_gui/src/main_logic/query_io_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions usermgmt_lib/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

pub use on_which_system::{OnSlurmLdapOnlyCli, OnWhichSystem, OnWhichSystemCli, OptFilePath};

mod on_which_system;
Expand Down Expand Up @@ -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<PathBuf>,
}

#[derive(Subcommand, Debug)]
Expand Down
13 changes: 12 additions & 1 deletion usermgmt_lib/src/config/path_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,18 @@ fn may_return_path_to_conf(
try_exists: impl Fn(&Path) -> io::Result<bool>,
path: &Path,
) -> Option<PathBuf> {
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 {
Expand Down

0 comments on commit ee54069

Please sign in to comment.