Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 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 @@ -35,6 +35,7 @@ quick-xml = "0.38.4"
quote = "1.0.42"
rand = "0.9.2"
rayon = "1.11.0"
regex = "1.12.2"
reqwest = { version = "0.12.26", default-features = false, features = ["json", "rustls-tls-native-roots", "http2"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
Expand Down
3 changes: 2 additions & 1 deletion pico_limbo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = { workspace = true }
futures = { workspace = true }
hmac = { workspace = true }
rand = { workspace = true }
regex = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand All @@ -26,4 +27,4 @@ thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-subscriber = { workspace = true }
11 changes: 8 additions & 3 deletions pico_limbo/src/configuration/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::configuration::boss_bar::BossBarConfig;
use crate::configuration::compression::CompressionConfig;
use crate::configuration::env_placeholders::expand_env_placeholders;
use crate::configuration::forwarding::ForwardingConfig;
use crate::configuration::game_mode_config::GameModeConfig;
use crate::configuration::server_list::ServerListConfig;
Expand All @@ -21,6 +22,9 @@ pub enum ConfigError {

#[error("TOML deserialization error: {0}")]
TomlDeserialize(#[from] toml::de::Error),

#[error("Missing environment variable: {0}")]
MissingEnvVar(String),
}

/// Application configuration, serializable to/from TOML.
Expand Down Expand Up @@ -100,12 +104,13 @@ pub fn load_or_create<P: AsRef<Path>>(path: P) -> Result<Config, ConfigError> {
let path = path.as_ref();

if path.exists() {
let toml_str = fs::read_to_string(path)?;
let raw_toml_str = fs::read_to_string(path)?;

if toml_str.trim().is_empty() {
if raw_toml_str.trim().is_empty() {
create_default_config(path)
} else {
let cfg: Config = toml::from_str(&toml_str)?;
let expanded_toml_str = expand_env_placeholders(&raw_toml_str)?;
let cfg: Config = toml::from_str(expanded_toml_str.as_ref())?;
Ok(cfg)
}
} else {
Expand Down
39 changes: 39 additions & 0 deletions pico_limbo/src/configuration/env_placeholders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use regex::{Captures, Regex};

use crate::configuration::config::ConfigError;
use std::{borrow::Cow, sync::LazyLock};

/// Expands environment placeholders in the given text.
///
/// Replaces occurrences of `${ENV_VAR}` with the corresponding value from the
/// process environment (via `std::env`). If a referenced variable is not set,
/// returns `ConfigError::MissingEnvVar`.
///
/// The sequence `\${` is treated as an escape and is converted to a literal `${`
/// without performing substitution.
pub fn expand_env_placeholders<'a>(input: &'a str) -> Result<Cow<'a, str>, ConfigError> {
static RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"(\\)?\$\{([A-Za-z_][A-Za-z0-9_]*)\}"#).unwrap());

for caps in RE.captures_iter(input) {
let is_escaped = caps.get(1).is_some();
let name = &caps[2];
if is_escaped {
continue;
}
if std::env::var(name).is_err() {
return Err(ConfigError::MissingEnvVar(name.to_string()));
}
}

let out = RE.replace_all(input, |caps: &Captures| {
let is_escaped = caps.get(1).is_some();
let name = &caps[2];
if is_escaped {
format!("${{{}}}", name)
} else {
std::env::var(name).unwrap()
}
});
Ok(out)
}
1 change: 1 addition & 0 deletions pico_limbo/src/configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod boss_bar;
mod compression;
pub mod config;
mod env_placeholders;
mod forwarding;
mod game_mode_config;
mod require_boolean;
Expand Down
6 changes: 6 additions & 0 deletions pico_limbo/src/server/start_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ fn load_configuration(config_path: &PathBuf) -> Option<Config> {
Err(ConfigError::Io(message, ..)) => {
error!("Failed to load configuration: {}", message);
}
Err(ConfigError::MissingEnvVar(var)) => {
error!(
"Failed to load configuration: missing environment variable {}",
var
);
}
Err(ConfigError::TomlSerialize(message, ..)) => {
error!("Failed to save default configuration file: {}", message);
}
Expand Down