diff --git a/doc/release-notes/iceoryx2-unreleased.md b/doc/release-notes/iceoryx2-unreleased.md index 7f7fe53ce..e96acdcc0 100644 --- a/doc/release-notes/iceoryx2-unreleased.md +++ b/doc/release-notes/iceoryx2-unreleased.md @@ -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 ``` diff --git a/iceoryx2-cli/Cargo.toml b/iceoryx2-cli/Cargo.toml index 5c4a10e7f..c803b5b4f 100644 --- a/iceoryx2-cli/Cargo.toml +++ b/iceoryx2-cli/Cargo.toml @@ -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 } diff --git a/iceoryx2-cli/iox2-config/src/cli.rs b/iceoryx2-cli/iox2-config/src/cli.rs index d6af1c26e..4dc524c02 100644 --- a/iceoryx2-cli/iox2-config/src/cli.rs +++ b/iceoryx2-cli/iox2-config/src/cli.rs @@ -30,10 +30,36 @@ pub struct Cli { pub action: Option, } +#[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, +} + +#[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, + }, #[clap(about = "Generate a default configuration file")] Generate, } diff --git a/iceoryx2-cli/iox2-config/src/commands.rs b/iceoryx2-cli/iox2-config/src/commands.rs index adcd2a7f7..b3d86fb7a 100644 --- a/iceoryx2-cli/iox2-config/src/commands.rs +++ b/iceoryx2-cli/iox2-config/src/commands.rs @@ -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. @@ -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::*; @@ -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"); diff --git a/iceoryx2-cli/iox2-config/src/main.rs b/iceoryx2-cli/iox2-config/src/main.rs index 178377d83..e1ce94b2c 100644 --- a/iceoryx2-cli/iox2-config/src/main.rs +++ b/iceoryx2-cli/iox2-config/src/main.rs @@ -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))] @@ -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); diff --git a/iceoryx2/Cargo.toml b/iceoryx2/Cargo.toml index ed536d038..08c1ec5b3 100644 --- a/iceoryx2/Cargo.toml +++ b/iceoryx2/Cargo.toml @@ -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 } diff --git a/iceoryx2/src/config.rs b/iceoryx2/src/config.rs index 35279519c..000d15917 100644 --- a/iceoryx2/src/config.rs +++ b/iceoryx2/src/config.rs @@ -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}; @@ -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; @@ -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() } }