Skip to content

Commit

Permalink
[eclipse-iceoryx#432] Add an abstraction layer to obtain the path to …
Browse files Browse the repository at this point in the history
…the configuration file + add a subcommand to display the contents of the configuration file
  • Loading branch information
brosier01 committed Nov 26, 2024
1 parent f6558d0 commit 41d20b8
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 28 deletions.
9 changes: 6 additions & 3 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

Create a new CLI for iceoryx2 `iox2-config`

`iox2 config` can `show` the configuration currently in use and `generate` a new
`iox2 config` can `show` the configuration of iceoryx currently in use
or `show` the configuration of the sytem 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)
Expand All @@ -22,7 +23,9 @@ Remove the `print_system_configuration()` function in
### New CLI features

```bash
cargo run --bin iox2-config show
iox2-config show system

iox2 config generate
iox2-config show current

iox2-config generate
```
1 change: 1 addition & 0 deletions iceoryx2-cal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ iceoryx2-bb-threadsafe = { workspace = true }
iceoryx2-bb-testing = { workspace = true }
iceoryx2-pal-concurrency-sync = { workspace = true }

dirs = { workspace = true }
once_cell = { workspace = true }
lazy_static = { workspace = true }
serde = { workspace = true }
Expand Down
35 changes: 35 additions & 0 deletions iceoryx2-cal/src/config_path/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! use iceoryx2_cal::config_path::config_dir;
//!
//! let config_dir = config_dir().unwrap();
//! println!("Config dir: {:?}", config_dir);
use dirs;

pub trait ConfigPathProvider {
fn config_dir(&self) -> Option<std::path::PathBuf>;
}

pub struct DirsConfigPathProvider;

impl ConfigPathProvider for DirsConfigPathProvider {
fn config_dir(&self) -> Option<std::path::PathBuf> {
dirs::config_dir()
}
}

pub fn config_dir() -> Option<std::path::PathBuf> {
let provider = DirsConfigPathProvider;
provider.config_dir()
}
1 change: 1 addition & 0 deletions iceoryx2-cal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

pub mod communication_channel;
pub mod config_path;
pub mod dynamic_storage;
pub mod event;
pub mod hash;
Expand Down
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,
}
16 changes: 14 additions & 2 deletions iceoryx2-cli/iox2-config/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use dialoguer::Confirm;
use enum_iterator::all;
use iceoryx2::config::Config;
use iceoryx2_bb_posix::system_configuration::*;
use iceoryx2_cal::config_path::config_dir;
use std::fs::{self, File};
use std::io::Write;
use std::panic::catch_unwind;
Expand Down Expand Up @@ -106,14 +107,25 @@ 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 json_config = serde_json::to_value(config).unwrap_or_else(|_| serde_json::json!({}));
let toml_config = toml::to_string_pretty(&json_config)
.unwrap_or_else(|_| "Failed to convert to TOML".to_string());

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: 0 additions & 1 deletion iceoryx2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ cdr = { workspace = true }
toml = { workspace = true }
sha1_smol = { workspace = true }
tiny-fn = { workspace = true }
dirs = { workspace = true }

[dev-dependencies]
iceoryx2-bb-testing = { workspace = true }
Expand Down
37 changes: 20 additions & 17 deletions iceoryx2/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,23 @@
//! # }
//! ```
use dirs;
use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_elementary::lazy_singleton::*;
use iceoryx2_bb_posix::{file::FileBuilder, shared_memory::AccessMode};
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_bb_system_types::file_path::FilePath;
use iceoryx2_bb_system_types::path::Path;
use iceoryx2_cal::config_path::config_dir;
use serde::{Deserialize, Serialize};
use std::{os::unix::ffi::OsStrExt, time::Duration};
use std::time::Duration;

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

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

/// Path to the default config file
pub const DEFAULT_CONFIG_FILE: &[u8] = b"config/iceoryx2.toml";

/// Failures occurring while creating a new [`Config`] object with [`Config::from_file()`] or
/// [`Config::setup_global_config_from_file()`]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -394,21 +397,21 @@ impl Config {
/// config was already populated.
pub fn global_config() -> &'static Config {
if !ICEORYX2_CONFIG.is_initialized()
&& Config::setup_global_config_from_file(unsafe {
&FilePath::new_unchecked(
dirs::config_dir()
.unwrap()
.join("iceoryx2")
.join("config.toml")
.as_os_str()
.as_bytes(),
)
})
.is_err()
{
warn!(from "Config::global_config()", "Default config file found but unable to read data, populate config with default values.");
ICEORYX2_CONFIG.set_value(Config::default());
if !ICEORYX2_CONFIG.is_initialized() {
let config_path = config_dir().unwrap().join("iceoryx2").join("config.toml");

match FilePath::new(config_path.as_os_str().as_encoded_bytes()) {
Ok(path) => {
if Config::setup_global_config_from_file(&path).is_err() {
warn!(from "Config::global_config()", "Default config file found but unable to read data, populate config with default values.");
ICEORYX2_CONFIG.set_value(Config::default());
}
}
Err(e) => {
warn!(from "Config::global_config()", "Error: {:?}", e);
ICEORYX2_CONFIG.set_value(Config::default());
}
}
}

ICEORYX2_CONFIG.get()
Expand Down

0 comments on commit 41d20b8

Please sign in to comment.