From 613a2e01d89ef87253ceb121cfd7ba0a33aa2d26 Mon Sep 17 00:00:00 2001 From: Bruce Rosier Date: Sun, 22 Dec 2024 23:16:14 +0100 Subject: [PATCH] [#432] Added error handling for config_dir() and moved dirs implementation to a separate file --- doc/release-notes/iceoryx2-unreleased.md | 8 +++--- iceoryx2-cal/src/config_path/dirs.rs | 29 ++++++++++++++++++++++ iceoryx2-cal/src/config_path/mod.rs | 17 ++----------- iceoryx2-cli/iox2-config/src/commands.rs | 2 +- iceoryx2/src/config.rs | 31 ++++++++++++++---------- 5 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 iceoryx2-cal/src/config_path/dirs.rs diff --git a/doc/release-notes/iceoryx2-unreleased.md b/doc/release-notes/iceoryx2-unreleased.md index 07ff6389b..3d1c91913 100644 --- a/doc/release-notes/iceoryx2-unreleased.md +++ b/doc/release-notes/iceoryx2-unreleased.md @@ -22,10 +22,8 @@ Remove the `print_system_configuration()` function in ### New CLI features -```bash - iox2-config show system +iox2 config show system - iox2-config show current +iox2 config show current - iox2-config generate -``` +iox2 config generate diff --git a/iceoryx2-cal/src/config_path/dirs.rs b/iceoryx2-cal/src/config_path/dirs.rs new file mode 100644 index 000000000..2de1d68cd --- /dev/null +++ b/iceoryx2-cal/src/config_path/dirs.rs @@ -0,0 +1,29 @@ +// 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 ::dirs; + +pub struct DirsConfigPathProvider; + +pub trait ConfigPathProvider { + fn config_dir() -> Option; +} + +impl ConfigPathProvider for DirsConfigPathProvider { + fn config_dir() -> Option { + dirs::config_dir() + } +} + +pub fn config_dir() -> Option { + DirsConfigPathProvider::config_dir() +} diff --git a/iceoryx2-cal/src/config_path/mod.rs b/iceoryx2-cal/src/config_path/mod.rs index 1a58d9de1..33d7978f8 100644 --- a/iceoryx2-cal/src/config_path/mod.rs +++ b/iceoryx2-cal/src/config_path/mod.rs @@ -15,21 +15,8 @@ //! let config_dir = config_dir().unwrap(); //! println!("Config dir: {:?}", config_dir); -use dirs; - -pub trait ConfigPathProvider { - fn config_dir(&self) -> Option; -} - -pub struct DirsConfigPathProvider; - -impl ConfigPathProvider for DirsConfigPathProvider { - fn config_dir(&self) -> Option { - dirs::config_dir() - } -} +pub mod dirs; pub fn config_dir() -> Option { - let provider = DirsConfigPathProvider; - provider.config_dir() + dirs::config_dir() } diff --git a/iceoryx2-cli/iox2-config/src/commands.rs b/iceoryx2-cli/iox2-config/src/commands.rs index 8c8074240..a4a219be0 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. diff --git a/iceoryx2/src/config.rs b/iceoryx2/src/config.rs index 558959688..85f005e3a 100644 --- a/iceoryx2/src/config.rs +++ b/iceoryx2/src/config.rs @@ -79,7 +79,7 @@ use iceoryx2_cal::config_path::config_dir; 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; @@ -404,25 +404,30 @@ impl Config { /// [`Config::setup_global_config_from_file()`] /// is called after this function was called, no file will be loaded since the global default /// config was already populated. - pub fn global_config() -> &'static Config { 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()); + 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) => { + if Config::setup_global_config_from_file(&path).is_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(e) => { - warn!(from "Config::global_config()", "Error: {:?}", e); + None => { + warn!(from "Config::global_config()", "Failed to retrieve config directory, using default config."); ICEORYX2_CONFIG.set_value(Config::default()); } - } + }; } - ICEORYX2_CONFIG.get() } }