Skip to content

Commit

Permalink
adding IBEJI_HOME env var for loading the invehicle digital twin conf…
Browse files Browse the repository at this point in the history
…ig file (#61)

* adding IBEJI_HOME env var for loading the invehicle digital twin config file

Signed-off-by: Leonardo Rossetti <[email protected]>

* use common::utils to load config file

Signed-off-by: Leonardo Rossetti <[email protected]>

* IBEJI_HOME env var docs

Signed-off-by: Leonardo Rossetti <[email protected]>

* fix variable name and readme typo

Signed-off-by: Leonardo Rossetti <[email protected]>

* move environment variable instructions

Signed-off-by: Leonardo Rossetti <[email protected]>

* adding code block language

Signed-off-by: Leonardo Rossetti <[email protected]>

* remove unecessary let

Signed-off-by: Leonardo Rossetti <[email protected]>

* static analysis fix

Signed-off-by: Leonardo Rossetti <[email protected]>

---------

Signed-off-by: Leonardo Rossetti <[email protected]>
  • Loading branch information
odra authored Oct 30, 2023
1 parent 4203b2c commit 27dffdb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ The demos use config files and we have provided a templated version of each conf
- {repo-root-dir}/core/invehicle-digital-twin/template
- {repo-root-dir}/samples/common/template

Configuration files will be loaded from the current working directory by default
but an `IBEJI_HOME` environment variable can be used to change the base configuration directory to a different one:

```bash
IBEJI_HOME=/etc/ibeji ./invehicle-digital-twin
```

The above example tells `invehicle-digital-twin` to load configuration files from `/etc/ibeji` instead of using
the current working directory.

Chariott may be used to discover the in-vehicle digital twin service. We will discuss how to enable this feature.

### <a name="property-sample">Property Sample</a>
Expand Down
12 changes: 10 additions & 2 deletions core/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use strum_macros::Display;
use tokio::time::{sleep, Duration};
use tonic::{Request, Status};

const IBEJI_HOME_VAR_NAME: &str = "IBEJI_HOME";

/// An identifier used when discovering a service through Chariott.
#[derive(Debug, Deserialize)]
pub struct ServiceIdentifier {
Expand Down Expand Up @@ -44,8 +46,14 @@ pub fn load_settings<T>(config_filename: &str) -> Result<T, ConfigError>
where
T: for<'de> serde::Deserialize<'de>,
{
let config =
Config::builder().add_source(File::new(config_filename, FileFormat::Yaml)).build()?;
let config_filename_path = match std::env::var(IBEJI_HOME_VAR_NAME) {
Ok(s) => format!("{}/{}", s, config_filename),
_ => config_filename.to_owned(),
};

let config = Config::builder()
.add_source(File::new(config_filename_path.as_str(), FileFormat::Yaml))
.build()?;

config.try_deserialize()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use config::{Config, File, FileFormat};
use common::utils;
use serde_derive::Deserialize;

const CONFIG_FILENAME: &str = "invehicle_digital_twin_settings";
Expand All @@ -15,10 +15,5 @@ pub struct Settings {

/// Load the settings.
pub fn load_settings() -> Settings {
let config =
Config::builder().add_source(File::new(CONFIG_FILENAME, FileFormat::Yaml)).build().unwrap();

let settings: Settings = config.try_deserialize().unwrap();

settings
utils::load_settings(CONFIG_FILENAME).unwrap()
}

0 comments on commit 27dffdb

Please sign in to comment.