Skip to content

Commit

Permalink
[#432] Added error handling for config_dir() + added a subcommand to …
Browse files Browse the repository at this point in the history
…display the contents of the configuration file + added configuration file standard location
  • Loading branch information
brosier01 committed Dec 26, 2024
1 parent 2d59ec1 commit 92982fb
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 33 deletions.
18 changes: 9 additions & 9 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

### Features

Create a new CLI for iceoryx2 `iox2-config`

`iox2 config` can `show` the configuration currently in use and `generate` a new
configuration file at the default location iceoryx2 is looking for.

* Add CLI to display complete system configuration [#432](https://github.com/eclipse-iceoryx/iceoryx2/issues/432)

### Refactoring

Remove the `print_system_configuration()` function in
* Remove the `print_system_configuration()` function in
`iceoryx2-bb/posix/src/system_configuration.rs` file and move it into the CLI `iox2-config`
[#432](https://github.com/eclipse-iceoryx/iceoryx2/issues/432)

### New CLI features

```bash
cargo run --bin iox2-config show
CLI can show the current iceoryx/system configuration or generate a iceoryx
configuration file.

```sh
iox2 config show system

iox2 config show current

iox2 config generate
iox2 config generate
```
1 change: 1 addition & 0 deletions iceoryx2-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ iceoryx2-pal-posix = { workspace = true }
iceoryx2-bb-posix = { workspace = true }
iceoryx2-bb-system-types = { workspace = true }
iceoryx2-bb-container = { workspace = true }
iceoryx2-cal = { workspace = true }

anyhow = { workspace = true }
better-panic = { workspace = true }
Expand Down
28 changes: 27 additions & 1 deletion iceoryx2-cli/iox2-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,36 @@ pub struct Cli {
pub action: Option<Action>,
}

#[derive(Parser)]
#[command(
name = "iox2-config",
about = "Query information about iceoryx2 configuration",
long_about = None,
version = env!("CARGO_PKG_VERSION"),
disable_help_subcommand = true,
arg_required_else_help = false,
help_template = help_template("iox2 config show", false),
)]
pub struct Config {
#[clap(subcommand)]
pub action: Option<ShowSubcommand>,
}

#[derive(Subcommand, Debug)]
pub enum ShowSubcommand {
#[clap(about = "Show system configuration")]
System,
#[clap(about = "Show current iceoryx2 configuration")]
Current,
}

#[derive(Subcommand)]
pub enum Action {
#[clap(about = "Show the currently used configuration")]
Show,
Show {
#[clap(subcommand)]
subcommand: Option<ShowSubcommand>,
},
#[clap(about = "Generate a default configuration file")]
Generate,
}
15 changes: 12 additions & 3 deletions iceoryx2-cli/iox2-config/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
Expand All @@ -13,6 +13,7 @@
use anyhow::Result;
use colored::Colorize;
use dialoguer::Confirm;
use dirs::config_dir;
use enum_iterator::all;
use iceoryx2::config::Config;
use iceoryx2_bb_posix::system_configuration::*;
Expand Down Expand Up @@ -106,14 +107,22 @@ pub fn print_system_configuration() {
}
}

pub fn show() -> Result<()> {
pub fn show_system_config() -> Result<()> {
print_system_configuration();

Ok(())
}

pub fn show_current_config() -> Result<()> {
let config = Config::global_config();
let toml_config = toml::to_string_pretty(&config)?;
println!("{}", toml_config);

Ok(())
}

pub fn generate() -> Result<()> {
let config_dir = dirs::config_dir().unwrap().join("iceoryx2/");
let config_dir = config_dir().unwrap().join("iceoryx2");
fs::create_dir_all(&config_dir)?;

let default_file_path = config_dir.join("config.toml");
Expand Down
22 changes: 18 additions & 4 deletions iceoryx2-cli/iox2-config/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use clap::CommandFactory;
use clap::Parser;
use cli::Action;
use cli::Cli;
use cli::Config;
use cli::ShowSubcommand;
use iceoryx2_bb_log::{set_log_level, LogLevel};

#[cfg(not(debug_assertions))]
Expand Down Expand Up @@ -44,11 +46,23 @@ fn main() {
Ok(cli) => {
if let Some(action) = cli.action {
match action {
Action::Show => {
if let Err(e) = commands::show() {
eprintln!("Failed to show options: {}", e);
Action::Show { subcommand } => match subcommand {
Some(ShowSubcommand::System) => {
if let Err(e) = commands::show_system_config() {
eprintln!("Failed to show options: {}", e);
}
}
}
Some(ShowSubcommand::Current) => {
if let Err(e) = commands::show_current_config() {
eprintln!("Failed to show options: {}", e);
}
}
None => {
Config::command()
.print_help()
.expect("Failed to print help");
}
},
Action::Generate => {
if let Err(e) = commands::generate() {
eprintln!("Failed to generate default configuration: {}", e);
Expand Down
1 change: 1 addition & 0 deletions iceoryx2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ iceoryx2-bb-elementary = { workspace = true }
iceoryx2-cal = { workspace = true }
iceoryx2-pal-concurrency-sync = { workspace = true }

dirs = { workspace = true }
lazy_static = { workspace = true }
serde = { workspace = true }
cdr = { workspace = true }
Expand Down
39 changes: 23 additions & 16 deletions iceoryx2/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
//! # }
//! ```
use dirs::config_dir;
use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_elementary::lazy_singleton::*;
use iceoryx2_bb_posix::{file::FileBuilder, shared_memory::AccessMode};
Expand All @@ -78,7 +79,7 @@ use iceoryx2_bb_system_types::path::Path;
use serde::{Deserialize, Serialize};
use std::time::Duration;

use iceoryx2_bb_log::{debug, fail, trace, warn};
use iceoryx2_bb_log::{fail, trace, warn};

use crate::service::port_factory::publisher::UnableToDeliverStrategy;

Expand Down Expand Up @@ -405,25 +406,31 @@ impl Config {
/// config was already populated.
pub fn global_config() -> &'static Config {
if !ICEORYX2_CONFIG.is_initialized() {
match Config::setup_global_config_from_file(unsafe {
&FilePath::new_unchecked(DEFAULT_CONFIG_FILE)
}) {
Ok(_) => (),
Err(ConfigCreationError::FailedToOpenConfigFile) => {
debug!(from "Config::global_config()", "Default config file not found, populate config with default values.");
ICEORYX2_CONFIG.set_value(Config::default());
}
Err(ConfigCreationError::FailedToReadConfigFileContents) => {
warn!(from "Config::global_config()", "Default config file found but unable to read content, populate config with default values.");
ICEORYX2_CONFIG.set_value(Config::default());
match config_dir() {
Some(dir) => {
let config_path = dir.join("iceoryx2").join("config.toml");
match FilePath::new(config_path.as_os_str().as_encoded_bytes()) {
Ok(path) => match Config::setup_global_config_from_file(&path) {
Ok(config) => {
ICEORYX2_CONFIG.set_value(config.clone());
}
Err(_) => {
warn!(from "Config::global_config()", "Default config file found but unable to read data, using default config.");
ICEORYX2_CONFIG.set_value(Config::default());
}
},
Err(_) => {
warn!(from "Config::global_config()", "Error creating FilePath, using default config.");
ICEORYX2_CONFIG.set_value(Config::default());
}
}
}
Err(ConfigCreationError::UnableToDeserializeContents) => {
warn!(from "Config::global_config()", "Default config file found but unable to load data, populate config with default values.");
None => {
warn!(from "Config::global_config()", "Failed to retrieve config directory, using default config.");
ICEORYX2_CONFIG.set_value(Config::default());
}
}
};
}

ICEORYX2_CONFIG.get()
}
}

0 comments on commit 92982fb

Please sign in to comment.