Skip to content

Commit

Permalink
Fix to use STU_ROOT_DIR environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Mar 16, 2024
1 parent c9d834e commit 43a7c48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ Or refer to the `***-help.png` screenshots in the [./img directory](./img).

### Config

Config is loaded from `~/.stu/config.toml`. If the file does not exist, it will be created automatically at startup.
Config is loaded from `$STU_ROOT_DIR/config.toml`.

- If `STU_ROOT_DIR` environment variable is not set, `~/.stu` is used by default.
- If the file does not exist, it will be created automatically at startup.

The values that can be set are as follows:

- `download_dir`: _string_ - Directory to save when downloading objects (_default_: `~/.stu/download`)
- `download_dir`: _string_ - Directory to save when downloading objects (_default_: `$STU_ROOT_DIR/download`)

## Screenshots

Expand Down
16 changes: 12 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::path::PathBuf;
use std::{env, path::PathBuf};

use serde_derive::{Deserialize, Serialize};

use crate::error::{AppError, Result};

const STU_ROOT_DIR_ENV_VAR: &str = "STU_ROOT_DIR";

const APP_BASE_DIR: &str = ".stu";
const CONFIG_FILE_NAME: &str = "config.toml";
const ERROR_LOG_FILE_NAME: &str = "error.log";
Expand Down Expand Up @@ -47,8 +49,14 @@ impl Config {
}

fn get_app_base_dir() -> Result<PathBuf> {
dirs::home_dir()
.map(|home| home.join(APP_BASE_DIR))
.ok_or_else(|| AppError::msg("Failed to load home directory"))
match env::var(STU_ROOT_DIR_ENV_VAR) {
Ok(dir) => Ok(PathBuf::from(dir)),
Err(_) => {
// default
dirs::home_dir()
.map(|home| home.join(APP_BASE_DIR))
.ok_or_else(|| AppError::msg("Failed to load home directory"))
}
}
}
}

0 comments on commit 43a7c48

Please sign in to comment.