Skip to content

Commit

Permalink
Customize config path with SLUMBER_CONFIG_PATH
Browse files Browse the repository at this point in the history
Closes #370
  • Loading branch information
LucasPickering committed Sep 9, 2024
1 parent 8bcafbe commit 7ebc263
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/slumber_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
27 changes: 24 additions & 3 deletions crates/slumber_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
);
}
}
2 changes: 1 addition & 1 deletion crates/slumber_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion docs/src/api/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ 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
```

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 |
Expand Down

0 comments on commit 7ebc263

Please sign in to comment.