Skip to content

Override configuration using environment variables #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tempdir = "0.3.4"
itertools = "0.7.4"
tempfile = "2.2.0"
shlex = "0.1.1"
toml-query = "0.6"

# Watch feature
notify = { version = "4.0", optional = true }
Expand Down
40 changes: 40 additions & 0 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,43 @@ additional-js = ["custom.js"]
editor = "./path/to/editor"
editable = false
```


## Environment Variables

All configuration values van be overridden from the command line by setting the
corresponding environment variable. Because many operating systems restrict
environment variables to be alphanumeric characters or `_`, the configuration
key needs to be formatted slightly differently to the normal `foo.bar.baz` form.

Variables starting with `MDBOOK_` are used for configuration. The key is
created by removing the `MDBOOK_` prefix and turning the resulting
string into `kebab-case`. Double underscores (`__`) separate nested
keys, while a single underscore (`_`) is replaced with a dash (`-`).

For example:

- `MDBOOK_foo` -> `foo`
- `MDBOOK_FOO` -> `foo`
- `MDBOOK_FOO__BAR` -> `foo.bar`
- `MDBOOK_FOO_BAR` -> `foo-bar`
- `MDBOOK_FOO_bar__baz` -> `foo-bar.baz`

So by setting the `MDBOOK_BOOK__TITLE` environment variable you can
override the book's title without needing to touch your `book.toml`.

> **Note:** To facilitate setting more complex config items, the value
> of an environment variable is first parsed as JSON, falling back to a
> string if the parse fails.
>
> This means, if you so desired, you could override all book metadata
> when building the book with something like
>
> ```text
> $ export MDBOOK_BOOK="{'title': 'My Awesome Book', authors: ['Michael-F-Bryan']}"
> $ mdbook build
> ```

The latter case may be useful in situations where `mdbook` is invoked
from a script or CI, where it sometimes isn't possible to update the
`book.toml` before building.
4 changes: 3 additions & 1 deletion src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ impl MDBook {
warn!("\thttps://rust-lang-nursery.github.io/mdBook/format/config.html");
}

let config = if config_location.exists() {
let mut config = if config_location.exists() {
debug!("[*] Loading config from {}", config_location.display());
Config::from_disk(&config_location)?
} else {
Config::default()
};

config.update_from_env();

if log_enabled!(::log::Level::Trace) {
for line in format!("Config: {:#?}", config).lines() {
trace!("{}", line);
Expand Down
Loading