Skip to content

Commit

Permalink
Merge pull request #88 from telefonicaid/kevin/env-simulation
Browse files Browse the repository at this point in the history
fix: setting .env variables on subservice
  • Loading branch information
fgalan committed Dec 1, 2022
2 parents 5cc4f41 + 9ac9149 commit e1455d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
@@ -0,0 +1 @@
- Fix: per-directory .env files
15 changes: 14 additions & 1 deletion bin/fiwareDeviceSimulatorCLI
Expand Up @@ -37,7 +37,20 @@ var path = require('path');
var request = require('request');
var time = require('time');
var deviceSimulator = require(ROOT_PATH + '/lib/fiwareDeviceSimulator');
require('dotenv').config()

require('dotenv').config({ path: getPathEnv() });

/**
* Helper function to search and set .env path, setting root where it will be searched the .env file, this has to be at the same level as the simulation.json file
* @return {String} .env Path
*/
function getPathEnv() {
const args = process.argv;
let simulationIndex = args.findIndex(item => item === '-c');
simulationIndex = args[simulationIndex + 1];
const path = simulationIndex.replace(/\/[a-zA-Z_-]+.json/g, '/.env');
return path;
}

/**
* Array with the last 10 emitted errors
Expand Down
16 changes: 16 additions & 0 deletions doc/manuals/simulation-configuration-file.md
Expand Up @@ -523,6 +523,22 @@ Last but not least, the [`./examples`](https://github.com/telefonicaid/fiware-de
The simulation configuration file may use an `.env` file to set configuration in the form of environment variables. This can be used to avoid setting
sensitive information, and be executed in whatever environment, having different configurations and not been needed to change anything on code.

Note that the `.env` file used by a given simulation file is the one that is located in the same directory where the simulation file is. For instance:

``` tree
├── electricvehicle # Subservice directory (simulation has to have the same configuration)
│ ├── .env # Environment variables (just for the simulations in this directory)
│ ├── simulation1.json # simulation1.json tests
│ └── simulation2.json # simulation2.json tests
│ └── ...
├── bathwaters # Subservice directory (simulation has to have the same configuration)
│ ├── .env # Environment variables (just for the simulations in this directory)
│ ├── simulation1.json # simulation1.json tests
│ └── simulation2.json # simulation2.json tests
│ └── ...
└── ...
```

Here's an example of `.env` file with the commonly environment variables used:

```
Expand Down

0 comments on commit e1455d8

Please sign in to comment.