Skip to content

Commit 27dffdb

Browse files
authored
adding IBEJI_HOME env var for loading the invehicle digital twin config file (#61)
* adding IBEJI_HOME env var for loading the invehicle digital twin config file Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * use common::utils to load config file Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * IBEJI_HOME env var docs Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * fix variable name and readme typo Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * move environment variable instructions Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * adding code block language Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * remove unecessary let Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> * static analysis fix Signed-off-by: Leonardo Rossetti <lrossett@redhat.com> --------- Signed-off-by: Leonardo Rossetti <lrossett@redhat.com>
1 parent 4203b2c commit 27dffdb

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ The demos use config files and we have provided a templated version of each conf
127127
- {repo-root-dir}/core/invehicle-digital-twin/template
128128
- {repo-root-dir}/samples/common/template
129129

130+
Configuration files will be loaded from the current working directory by default
131+
but an `IBEJI_HOME` environment variable can be used to change the base configuration directory to a different one:
132+
133+
```bash
134+
IBEJI_HOME=/etc/ibeji ./invehicle-digital-twin
135+
```
136+
137+
The above example tells `invehicle-digital-twin` to load configuration files from `/etc/ibeji` instead of using
138+
the current working directory.
139+
130140
Chariott may be used to discover the in-vehicle digital twin service. We will discuss how to enable this feature.
131141

132142
### <a name="property-sample">Property Sample</a>

core/common/src/utils.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use strum_macros::Display;
1616
use tokio::time::{sleep, Duration};
1717
use tonic::{Request, Status};
1818

19+
const IBEJI_HOME_VAR_NAME: &str = "IBEJI_HOME";
20+
1921
/// An identifier used when discovering a service through Chariott.
2022
#[derive(Debug, Deserialize)]
2123
pub struct ServiceIdentifier {
@@ -44,8 +46,14 @@ pub fn load_settings<T>(config_filename: &str) -> Result<T, ConfigError>
4446
where
4547
T: for<'de> serde::Deserialize<'de>,
4648
{
47-
let config =
48-
Config::builder().add_source(File::new(config_filename, FileFormat::Yaml)).build()?;
49+
let config_filename_path = match std::env::var(IBEJI_HOME_VAR_NAME) {
50+
Ok(s) => format!("{}/{}", s, config_filename),
51+
_ => config_filename.to_owned(),
52+
};
53+
54+
let config = Config::builder()
55+
.add_source(File::new(config_filename_path.as_str(), FileFormat::Yaml))
56+
.build()?;
4957

5058
config.try_deserialize()
5159
}

core/invehicle-digital-twin/src/invehicle_digital_twin_config.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license.
33
// SPDX-License-Identifier: MIT
44

5-
use config::{Config, File, FileFormat};
5+
use common::utils;
66
use serde_derive::Deserialize;
77

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

1616
/// Load the settings.
1717
pub fn load_settings() -> Settings {
18-
let config =
19-
Config::builder().add_source(File::new(CONFIG_FILENAME, FileFormat::Yaml)).build().unwrap();
20-
21-
let settings: Settings = config.try_deserialize().unwrap();
22-
23-
settings
18+
utils::load_settings(CONFIG_FILENAME).unwrap()
2419
}

0 commit comments

Comments
 (0)