Skip to content

Commit

Permalink
BACON_PREFS and BACON_CONFIG env vars
Browse files Browse the repository at this point in the history
Fix #76
  • Loading branch information
Canop committed Oct 6, 2024
1 parent 861f6b3 commit af350ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- new `exports` structure in configuration. New `analysis` export bound by default to `ctrl-e`. The old syntax defining locations export is still supported but won't appear in documentations anymore.
- recognize panic location in test - Fix #208
- `toggle-backtrace` accepts an optional level: `toggle-backtrace(1)` or `toggle-backtrace(full)` - Experimental - Fix #210
- configuration can be passed in `BACON_PREFS` and `BACON_CONFIG` env vars - Fix #76

<a name="v2.21.0"></a>
### v2.21.0 - 2024/09/14
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ pub fn run() -> anyhow::Result<()> {
}
}

if let Some(config) = Config::from_env("BACON_PREFS")? {
settings.apply_config(&config);
}

let location = MissionLocation::new(&args)?;
info!("mission location: {:#?}", &location);

Expand Down Expand Up @@ -102,6 +106,10 @@ pub fn run() -> anyhow::Result<()> {
settings.apply_config(&config);
}

if let Some(config) = Config::from_env("BACON_CONFIG")? {
settings.apply_config(&config);
}

// args are applied after prefs, and package config so that they can override them
settings.apply_args(&args);

Expand Down
23 changes: 22 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
},
};

/// A configuration item which may be stored either as `bacon.toml`
/// A configuration item which may be stored in various places, eg as `bacon.toml`
/// along a `Cargo.toml` file or as `prefs.toml` in the xdg config directory.
///
/// Leaf values are options (and not Default) so that they don't
Expand Down Expand Up @@ -66,6 +66,27 @@ impl Config {
}
Ok(conf)
}
pub fn from_env(env_var_name: &str) -> Result<Option<Self>> {
let Some(path) = std::env::var_os(env_var_name) else {
return Ok(None);
};
let path = Path::new(&path);
if !path.exists() {
// some users may want to use an env var to point to a file that may not always exist
// so we don't bail here
warn!(
"Env var {:?} points to file {:?} which does not exist",
env_var_name, path
);
return Ok(None);
}
let config = Self::from_path(path)?;
debug!(
"Loaded config at {:?} as specified in env var {:?}",
path, env_var_name
);
Ok(Some(config))
}
pub fn default_package_config() -> Self {
toml::from_str(DEFAULT_PACKAGE_CONFIG).unwrap()
}
Expand Down
15 changes: 11 additions & 4 deletions website/docs/config.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@

# Configuration Files

The behavior of bacon is defined by a global `prefs.toml` file and project specific `bacon.toml` files.

All configuration files are optional but you'll soon need specific jobs for your targets, examples, etc.
All configuration files are optional but you'll probably need specific jobs for your targets, examples, etc.

All accept the same properties (preferences, keybindings, jobs, etc.).
The properties of the global `prefs.toml` file are overriden by the workspace level `bacon.toml` file, then by the package level `bacon.toml` file.

Bacon loads its default configuration then, in order:

* the global `prefs.toml` ([global preferences](#global-preferences))
* the file whose path is in environment variable `BACON_PREFS`
* the workspace level `bacon.toml` file
* the package level `bacon.toml` file ([project settings](#project-settings))
* the file whose path is in environment variable `BACON_CONFIG`

Each configuration file overrides the properties of previously loaded ones.

When you modified those files and bacon evolved since, you may want to have a look at the current default ones and pick the changes you like:

Expand Down

0 comments on commit af350ff

Please sign in to comment.