From 43a7c48b8adb65c95039e08f7ab13db3571fa0fa Mon Sep 17 00:00:00 2001 From: Kyosuke Fujimoto Date: Sat, 16 Mar 2024 09:44:38 +0900 Subject: [PATCH] Fix to use STU_ROOT_DIR environment variable --- README.md | 7 +++++-- src/config.rs | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9b6e44b..c9ca72c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/config.rs b/src/config.rs index 3c346ba..cd69f35 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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"; @@ -47,8 +49,14 @@ impl Config { } fn get_app_base_dir() -> Result { - 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")) + } + } } }