[chore](trx-app): drop per-binary config file support
Only trx-rs.toml with [trx-server] / [trx-client] section headers is now supported. Simplify ConfigFile trait to a single required method section_key(); remove config_filename(), combined_key(), and default_search_paths(). load_from_file() now errors when the expected section is absent rather than falling back to flat parsing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Stan Grams <sjg@haxx.space>
This commit is contained in:
+22
-67
@@ -15,9 +15,9 @@ pub enum ConfigError {
|
|||||||
ParseError(PathBuf, String),
|
ParseError(PathBuf, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns search paths for the combined `trx-rs.toml` config file
|
/// Returns the default search paths for `trx-rs.toml`
|
||||||
/// (current directory → XDG config → /etc).
|
/// (current directory → XDG config → /etc).
|
||||||
pub fn combined_config_paths() -> Vec<PathBuf> {
|
fn config_search_paths() -> Vec<PathBuf> {
|
||||||
let mut paths = vec![PathBuf::from("trx-rs.toml")];
|
let mut paths = vec![PathBuf::from("trx-rs.toml")];
|
||||||
if let Some(config_dir) = dirs::config_dir() {
|
if let Some(config_dir) = dirs::config_dir() {
|
||||||
paths.push(config_dir.join("trx-rs").join("trx-rs.toml"));
|
paths.push(config_dir.join("trx-rs").join("trx-rs.toml"));
|
||||||
@@ -52,82 +52,37 @@ fn load_section_from_file<T: DeserializeOwned>(
|
|||||||
Ok(Some(cfg))
|
Ok(Some(cfg))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for loading configuration files with default paths.
|
/// Trait for loading configuration from a `trx-rs.toml` section.
|
||||||
pub trait ConfigFile: Sized + Default + DeserializeOwned {
|
pub trait ConfigFile: Sized + Default + DeserializeOwned {
|
||||||
/// Config filename (e.g., "server.toml" or "client.toml")
|
/// Section key in `trx-rs.toml` (e.g. `"trx-server"` or `"trx-client"`).
|
||||||
fn config_filename() -> &'static str;
|
fn section_key() -> &'static str;
|
||||||
|
|
||||||
/// Section key inside a combined `trx-rs.toml` file, e.g. `"trx-server"`.
|
/// Load the section from a specific file path.
|
||||||
/// Return `None` (the default) to disable combined-file support.
|
|
||||||
fn combined_key() -> Option<&'static str> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Load config from a specific file path.
|
|
||||||
///
|
///
|
||||||
/// If `combined_key()` is set and the file contains that section header,
|
/// Returns an error if the file cannot be read, is not valid TOML, or
|
||||||
/// only that section is deserialized. Otherwise the whole file is used,
|
/// does not contain the expected `[<section_key>]` header.
|
||||||
/// preserving full backward compatibility with per-binary config files.
|
|
||||||
fn load_from_file(path: &Path) -> Result<Self, ConfigError> {
|
fn load_from_file(path: &Path) -> Result<Self, ConfigError> {
|
||||||
if let Some(key) = Self::combined_key() {
|
load_section_from_file::<Self>(path, Self::section_key())?.ok_or_else(|| {
|
||||||
// Peek at the file: if it contains our section, use that section.
|
ConfigError::ParseError(
|
||||||
if let Ok(Some(cfg)) = load_section_from_file::<Self>(path, key) {
|
path.to_path_buf(),
|
||||||
return Ok(cfg);
|
format!("missing [{}] section", Self::section_key()),
|
||||||
}
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
let content = std::fs::read_to_string(path)
|
|
||||||
.map_err(|e| ConfigError::ReadError(path.to_path_buf(), e.to_string()))?;
|
|
||||||
toml::from_str(&content)
|
|
||||||
.map_err(|e| ConfigError::ParseError(path.to_path_buf(), e.to_string()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search default paths and load first found config.
|
/// Search default paths (`trx-rs.toml` in CWD → XDG → /etc) and load
|
||||||
|
/// the first file that contains the expected section.
|
||||||
///
|
///
|
||||||
/// Search order (for each location tier — CWD, XDG, /etc):
|
/// Returns `(config, path_where_found)` or `(Default::default(), None)`
|
||||||
/// 1. `trx-rs.toml` with our section header (combined file)
|
/// when no config file is found.
|
||||||
/// 2. per-binary flat file (e.g. `trx-server.toml`)
|
|
||||||
///
|
|
||||||
/// Returns `(config, path_where_found)` or `(Default::default(), None)`.
|
|
||||||
fn load_from_default_paths() -> Result<(Self, Option<PathBuf>), ConfigError> {
|
fn load_from_default_paths() -> Result<(Self, Option<PathBuf>), ConfigError> {
|
||||||
let combined = combined_config_paths();
|
for path in config_search_paths() {
|
||||||
let flat = Self::default_search_paths();
|
if path.exists() {
|
||||||
|
if let Some(cfg) = load_section_from_file::<Self>(&path, Self::section_key())? {
|
||||||
// Build interleaved list: (combined_path, flat_path) per tier.
|
return Ok((cfg, Some(path)));
|
||||||
let tiers = combined.len().max(flat.len());
|
|
||||||
for i in 0..tiers {
|
|
||||||
// Combined file at this tier
|
|
||||||
if let Some(key) = Self::combined_key() {
|
|
||||||
if let Some(path) = combined.get(i) {
|
|
||||||
if path.exists() {
|
|
||||||
if let Some(cfg) = load_section_from_file::<Self>(path, key)? {
|
|
||||||
return Ok((cfg, Some(path.clone())));
|
|
||||||
}
|
|
||||||
// Combined file present but our section absent → skip to flat.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Flat file at this tier
|
|
||||||
if let Some(path) = flat.get(i) {
|
|
||||||
if path.exists() {
|
|
||||||
let cfg = Self::load_from_file(path)?;
|
|
||||||
return Ok((cfg, Some(path.clone())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok((Self::default(), None))
|
Ok((Self::default(), None))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default search paths for the per-binary flat config file
|
|
||||||
/// (current dir → XDG → /etc).
|
|
||||||
fn default_search_paths() -> Vec<PathBuf> {
|
|
||||||
let mut paths = vec![PathBuf::from(Self::config_filename())];
|
|
||||||
|
|
||||||
if let Some(config_dir) = dirs::config_dir() {
|
|
||||||
paths.push(config_dir.join("trx-rs").join(Self::config_filename()));
|
|
||||||
}
|
|
||||||
|
|
||||||
paths.push(PathBuf::from("/etc/trx-rs").join(Self::config_filename()));
|
|
||||||
paths
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub mod logging;
|
|||||||
pub mod plugins;
|
pub mod plugins;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
pub use config::{combined_config_paths, ConfigError, ConfigFile};
|
pub use config::{ConfigError, ConfigFile};
|
||||||
pub use logging::init_logging;
|
pub use logging::init_logging;
|
||||||
pub use plugins::{load_backend_plugins, load_frontend_plugins};
|
pub use plugins::{load_backend_plugins, load_frontend_plugins};
|
||||||
pub use util::normalize_name;
|
pub use util::normalize_name;
|
||||||
|
|||||||
@@ -4,11 +4,12 @@
|
|||||||
|
|
||||||
//! Configuration file support for trx-client.
|
//! Configuration file support for trx-client.
|
||||||
//!
|
//!
|
||||||
//! Supports loading configuration from TOML files with the following search order:
|
//! Config is loaded from the `[trx-client]` section of `trx-rs.toml`.
|
||||||
|
//! Default search order:
|
||||||
//! 1. Path specified via `--config` CLI argument
|
//! 1. Path specified via `--config` CLI argument
|
||||||
//! 2. `./trx-rs.toml` `[trx-client]` section, or `./trx-client.toml`
|
//! 2. `./trx-rs.toml`
|
||||||
//! 3. `~/.config/trx-rs/trx-rs.toml` `[trx-client]` section, or `~/.config/trx-rs/client.toml`
|
//! 3. `~/.config/trx-rs/trx-rs.toml`
|
||||||
//! 4. `/etc/trx-rs/trx-rs.toml` `[trx-client]` section, or `/etc/trx-rs/client.toml`
|
//! 4. `/etc/trx-rs/trx-rs.toml`
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
@@ -482,22 +483,8 @@ fn validate_http_auth(auth: &HttpAuthConfig) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigFile for ClientConfig {
|
impl ConfigFile for ClientConfig {
|
||||||
fn config_filename() -> &'static str {
|
fn section_key() -> &'static str {
|
||||||
"client.toml"
|
"trx-client"
|
||||||
}
|
|
||||||
|
|
||||||
fn combined_key() -> Option<&'static str> {
|
|
||||||
Some("trx-client")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_search_paths() -> Vec<PathBuf> {
|
|
||||||
let mut paths = Vec::new();
|
|
||||||
paths.push(PathBuf::from("trx-client.toml"));
|
|
||||||
if let Some(config_dir) = dirs::config_dir() {
|
|
||||||
paths.push(config_dir.join("trx-rs").join("client.toml"));
|
|
||||||
}
|
|
||||||
paths.push(PathBuf::from("/etc/trx-rs/client.toml"));
|
|
||||||
paths
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,12 @@
|
|||||||
|
|
||||||
//! Configuration file support for trx-server.
|
//! Configuration file support for trx-server.
|
||||||
//!
|
//!
|
||||||
//! Supports loading configuration from TOML files with the following search order:
|
//! Config is loaded from the `[trx-server]` section of `trx-rs.toml`.
|
||||||
|
//! Default search order:
|
||||||
//! 1. Path specified via `--config` CLI argument
|
//! 1. Path specified via `--config` CLI argument
|
||||||
//! 2. `./trx-rs.toml` `[trx-server]` section, or `./trx-server.toml`
|
//! 2. `./trx-rs.toml`
|
||||||
//! 3. `~/.config/trx-rs/trx-rs.toml` `[trx-server]` section, or `~/.config/trx-rs/server.toml`
|
//! 3. `~/.config/trx-rs/trx-rs.toml`
|
||||||
//! 4. `/etc/trx-rs/trx-rs.toml` `[trx-server]` section, or `/etc/trx-rs/server.toml`
|
//! 4. `/etc/trx-rs/trx-rs.toml`
|
||||||
|
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -789,22 +790,8 @@ fn validate_tokens(path: &str, tokens: &[String]) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigFile for ServerConfig {
|
impl ConfigFile for ServerConfig {
|
||||||
fn config_filename() -> &'static str {
|
fn section_key() -> &'static str {
|
||||||
"server.toml"
|
"trx-server"
|
||||||
}
|
|
||||||
|
|
||||||
fn combined_key() -> Option<&'static str> {
|
|
||||||
Some("trx-server")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_search_paths() -> Vec<PathBuf> {
|
|
||||||
let mut paths = Vec::new();
|
|
||||||
paths.push(PathBuf::from("trx-server.toml"));
|
|
||||||
if let Some(config_dir) = dirs::config_dir() {
|
|
||||||
paths.push(config_dir.join("trx-rs").join("server.toml"));
|
|
||||||
}
|
|
||||||
paths.push(PathBuf::from("/etc/trx-rs/server.toml"));
|
|
||||||
paths
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user