diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b2f529a..eada147f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ## [Unreleased] - ReleaseDate +### Added + +- Use `SLUMBER_CONFIG_PATH` to customize configuration (_not_ collection) file path [#370](https://github.com/LucasPickering/slumber/issues/370) + ### Fixed -- Updated the Configuration docs to remove the non-existent `slumber show dir` command +- Updated the Configuration docs to remove the non-existent `slumber show dir` command (thanks @SVendittelli) ## [2.0.0] - 2024-09-06 diff --git a/Cargo.lock b/Cargo.lock index 12f271bb..8779937b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2218,6 +2218,7 @@ dependencies = [ "anyhow", "crossterm", "derive_more", + "env-lock", "indexmap", "itertools 0.13.0", "ratatui", diff --git a/Cargo.toml b/Cargo.toml index d3d499d1..0175f122 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ bytes = {version = "1.6.1", default-features = false} chrono = {version = "0.4.31", default-features = false} crossterm = {version = "0.28.0", default-features = false, features = ["events"]} derive_more = {version = "1.0.0", default-features = false} +env-lock = "0.1.0" futures = "0.3.28" indexmap = {version = "2.0.0", default-features = false} itertools = "0.13.0" diff --git a/crates/slumber_config/Cargo.toml b/crates/slumber_config/Cargo.toml index e1447e45..95ad7603 100644 --- a/crates/slumber_config/Cargo.toml +++ b/crates/slumber_config/Cargo.toml @@ -22,6 +22,7 @@ slumber_core = {workspace = true} tracing = {workspace = true} [dev-dependencies] +env-lock = {workspace = true} rstest = {workspace = true} serde_test = {workspace = true} slumber_core = {workspace = true, features = ["test"]} diff --git a/crates/slumber_config/src/lib.rs b/crates/slumber_config/src/lib.rs index ed4c90f1..7d90ac5f 100644 --- a/crates/slumber_config/src/lib.rs +++ b/crates/slumber_config/src/lib.rs @@ -20,11 +20,12 @@ use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use slumber_core::{ http::HttpEngineConfig, - util::{parse_yaml, DataDirectory, ResultTraced}, + util::{expand_home, parse_yaml, DataDirectory, ResultTraced}, }; -use std::{fs::File, path::PathBuf}; +use std::{env, fs::File, path::PathBuf}; use tracing::info; +const PATH_ENV_VAR: &str = "SLUMBER_CONFIG_PATH"; const FILE: &str = "config.yml"; /// App-level configuration, which is global across all sessions and @@ -52,7 +53,9 @@ pub struct Config { impl Config { /// Path to the configuration file pub fn path() -> PathBuf { - DataDirectory::get().file(FILE) + env::var(PATH_ENV_VAR) + .map(|path| expand_home(PathBuf::from(path)).into_owned()) + .unwrap_or_else(|_| DataDirectory::get().file(FILE)) } /// Load configuration from the file, if present. If not, just return a @@ -96,3 +99,21 @@ impl Default for Config { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_custom_config_path() { + let _guard = env_lock::lock_env([ + ("HOME", Some("/root")), + (PATH_ENV_VAR, Some("~/dotfiles/slumber.yml")), + ]); + // Note: tilde is NOT expanded here; we expect the shell to do that + assert_eq!( + Config::path().to_str().unwrap(), + "/root/dotfiles/slumber.yml" + ); + } +} diff --git a/crates/slumber_core/Cargo.toml b/crates/slumber_core/Cargo.toml index b0b74f7e..85427d0d 100644 --- a/crates/slumber_core/Cargo.toml +++ b/crates/slumber_core/Cargo.toml @@ -41,7 +41,7 @@ uuid = {workspace = true, features = ["serde", "v4"]} winnow = "0.6.16" [dev-dependencies] -env-lock = "0.1.0" +env-lock = {workspace = true} pretty_assertions = {workspace = true} proptest = "1.5.0" proptest-derive = "0.5.0" diff --git a/docs/src/api/configuration/index.md b/docs/src/api/configuration/index.md index a77f55ad..8e5ba7b2 100644 --- a/docs/src/api/configuration/index.md +++ b/docs/src/api/configuration/index.md @@ -4,7 +4,7 @@ Configuration provides _application_-level settings, as opposed to collection-le ## Location & Creation -Configuration is stored in the Slumber root directory, under the file `config.yml`. To find the config file, you can run: +By default, configuration is stored in the Slumber root directory, under the file `config.yml`. To find the config file, you can run: ```sh slumber show paths @@ -12,6 +12,12 @@ slumber show paths If the root directory doesn't exist yet, you can create it yourself or have Slumber create it by simply starting the TUI. +You can change the location of the config file by setting the environment variable `SLUMBER_CONFIG_PATH`. For example: + +```sh +SLUMBER_CONFIG_PATH=~/dotfiles/slumber.yml slumber +``` + ## Fields | Field | Type | Description | Default |