mirror of
https://github.com/rust-lang/mdBook.git
synced 2025-12-27 10:16:09 -05:00
Merge pull request #2952 from ehuss/ignore-top-env-keys
Ignore invalid top-level environment variable config keys
This commit is contained in:
@@ -43,6 +43,7 @@
|
|||||||
//! # run().unwrap()
|
//! # run().unwrap()
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
use crate::static_regex;
|
||||||
use crate::utils::{TomlExt, fs, log_backtrace};
|
use crate::utils::{TomlExt, fs, log_backtrace};
|
||||||
use anyhow::{Context, Error, Result, bail};
|
use anyhow::{Context, Error, Result, bail};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -149,15 +150,23 @@ impl Config {
|
|||||||
pub fn update_from_env(&mut self) -> Result<()> {
|
pub fn update_from_env(&mut self) -> Result<()> {
|
||||||
debug!("Updating the config from environment variables");
|
debug!("Updating the config from environment variables");
|
||||||
|
|
||||||
|
static_regex!(
|
||||||
|
VALID_KEY,
|
||||||
|
r"^(:?book|build|rust|output|preprocessor)(:?$|\.)"
|
||||||
|
);
|
||||||
|
|
||||||
let overrides =
|
let overrides =
|
||||||
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));
|
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));
|
||||||
|
|
||||||
for (key, value) in overrides {
|
for (key, value) in overrides {
|
||||||
if key == "log" {
|
trace!("{} => {}", key, value);
|
||||||
// MDBOOK_LOG is used to control logging.
|
if !VALID_KEY.is_match(&key) {
|
||||||
|
// Ignore environment variables for other top-level things.
|
||||||
|
// This allows users to set things like `MDBOOK_VERSION` or
|
||||||
|
// `MDBOOK_DOWNLOAD_URL` for their own scripts and not
|
||||||
|
// interfere with how the config is loaded.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
trace!("{} => {}", key, value);
|
|
||||||
let parsed_value = serde_json::from_str(&value)
|
let parsed_value = serde_json::from_str(&value)
|
||||||
.unwrap_or_else(|_| serde_json::Value::String(value.to_string()));
|
.unwrap_or_else(|_| serde_json::Value::String(value.to_string()));
|
||||||
|
|
||||||
|
|||||||
@@ -213,10 +213,11 @@ unknown field `title`, expected `edition`
|
|||||||
fn env_invalid_config_key() {
|
fn env_invalid_config_key() {
|
||||||
BookTest::from_dir("config/empty").run("build", |cmd| {
|
BookTest::from_dir("config/empty").run("build", |cmd| {
|
||||||
cmd.env("MDBOOK_FOO", "testing")
|
cmd.env("MDBOOK_FOO", "testing")
|
||||||
.expect_failure()
|
|
||||||
.expect_stdout(str![[""]])
|
.expect_stdout(str![[""]])
|
||||||
.expect_stderr(str![[r#"
|
.expect_stderr(str![[r#"
|
||||||
ERROR invalid key `foo`
|
INFO Book building has started
|
||||||
|
INFO Running the html backend
|
||||||
|
INFO HTML book written to `[ROOT]/book`
|
||||||
|
|
||||||
"#]]);
|
"#]]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user