Skip to content

Commit

Permalink
feat: fallback to current directory when HOME is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfdl committed Jun 18, 2024
1 parent 92fcaf8 commit 0cc7d1e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Before you begin, ensure you have:
```sh
./tagoio-relay init
```
2. **Edit Config File**: Modify the generated `config.toml` as described in the [Configuration File and Environment Variables](#configuration-file-and-environment-variables).
2. **Edit Config File**: Modify the generated `.tagoio-mqtt-relay.toml` as described in the [Configuration File and Environment Variables](#configuration-file-and-environment-variables).
3. **Start the Relay**:
```sh
Expand Down Expand Up @@ -110,7 +110,7 @@ The CLI has two main commands: `init` and `start`.
### `init`
Generates the `config.toml` file required for setting up the Relay.
Generates the `.tagoio-mqtt-relay.toml` file required for setting up the Relay.
```sh
tagoio-relay init [--config-path /path/to/config]
Expand All @@ -121,7 +121,7 @@ tagoio-relay init [--config-path /path/to/config]
Starts the MQTT Relay service.
```sh
tagoio-relay start [--verbose info,mqtt] [--config-path /path/to/config.toml]
tagoio-relay start [--verbose info,error,mqtt,network] [--config-path /path/to/config.toml]
```
## Configuration File and Environment Variables
Expand All @@ -145,11 +145,12 @@ export TAGOIO__RELAY__MQTT__PASSWORD="my-password"
export TAGOIO__RELAY__MQTT__BROKER_TLS_CA=""
export TAGOIO__RELAY__MQTT__BROKER_TLS_CERT=""
export TAGOIO__RELAY__MQTT__BROKER_TLS_KEY=""
export TAGOIO__RELAY__CONFIG_PATH="./.config/.tagoio-mqtt-relay.toml"
```
### `config.toml`
### `.tagoio-mqtt-relay.toml`
The `config.toml` file contains the Relay parameters. Here is a reference:
The `.tagoio-mqtt-relay.toml` file contains the Relay parameters. Here is a reference:
```toml
[relay]
Expand Down
2 changes: 1 addition & 1 deletion examples/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ services:
ports:
- "3001:3001"
volumes:
- ./config.toml:/root/.config/.tagoio-mqtt-relay.toml
- ./config.toml:./tagoio-mqtt-relay.toml
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DEFAULT_CONFIG: &str = include_str!("./default_config.toml");
*/
fn get_config_path(user_path: Option<String>) -> std::path::PathBuf {
let env_config_path = if user_path.is_none() {
std::env::var("CONFIG_PATH").ok()
std::env::var("TAGOIO__RELAY__CONFIG_PATH").ok()
} else {
user_path
};
Expand All @@ -25,7 +25,10 @@ fn get_config_path(user_path: Option<String>) -> std::path::PathBuf {
.as_deref()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
let home_dir = home_dir().expect("Failed to get home directory");
let home_dir = home_dir().unwrap_or_else(|| {
log::warn!(target: "info", "Home directory is not set. Using current directory as fallback.");
std::path::PathBuf::from(".")
});
let config_path_str = format!("{}/.config/.tagoio-mqtt-relay.toml", home_dir.display());
std::path::PathBuf::from(config_path_str)
});
Expand Down

0 comments on commit 0cc7d1e

Please sign in to comment.