Skip to content

Commit

Permalink
[eclipse-iceoryx#432] Added error handling for config_dir() and moved…
Browse files Browse the repository at this point in the history
… dirs implementation to a separate file
  • Loading branch information
brosier01 committed Dec 24, 2024
1 parent 6ea6962 commit d28c6fb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
8 changes: 3 additions & 5 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions iceoryx2-cal/src/config_path/dirs.rs
Original file line number Diff line number Diff line change
@@ -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<std::path::PathBuf>;
}

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

pub fn config_dir() -> Option<std::path::PathBuf> {
DirsConfigPathProvider::config_dir()
}
17 changes: 2 additions & 15 deletions iceoryx2-cal/src/config_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::path::PathBuf>;
}

pub struct DirsConfigPathProvider;

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

pub fn config_dir() -> Option<std::path::PathBuf> {
let provider = DirsConfigPathProvider;
provider.config_dir()
dirs::config_dir()
}
31 changes: 18 additions & 13 deletions iceoryx2/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
}

0 comments on commit d28c6fb

Please sign in to comment.