From 2fb43dd38dfa1a86632921ebb65ca10f34093e33 Mon Sep 17 00:00:00 2001
From: Jason Volk <jason@zemos.net>
Date: Mon, 22 Jul 2024 21:16:46 +0000
Subject: [PATCH] infra to synthesize program options with config options

Signed-off-by: Jason Volk <jason@zemos.net>
---
 src/core/config/mod.rs |  2 +-
 src/main/clap.rs       | 12 +++++++++++-
 src/main/main.rs       |  2 +-
 src/main/server.rs     |  5 +++--
 4 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs
index b40ebb65f..a689701aa 100644
--- a/src/core/config/mod.rs
+++ b/src/core/config/mod.rs
@@ -405,7 +405,7 @@ struct ListeningAddr {
 
 impl Config {
 	/// Initialize config
-	pub fn new(path: Option<PathBuf>) -> Result<Self> {
+	pub fn new(path: &Option<PathBuf>) -> Result<Self> {
 		let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") {
 			Figment::new()
 				.merge(Toml::file(config_file_env).nested())
diff --git a/src/main/clap.rs b/src/main/clap.rs
index 6ce164f53..e82fea16e 100644
--- a/src/main/clap.rs
+++ b/src/main/clap.rs
@@ -3,6 +3,7 @@
 use std::path::PathBuf;
 
 use clap::Parser;
+use conduit::{Config, Result};
 
 /// Commandline arguments
 #[derive(Parser, Debug)]
@@ -15,4 +16,13 @@ pub(crate) struct Args {
 
 /// Parse commandline arguments into structured data
 #[must_use]
-pub(crate) fn parse() -> Args { Args::parse() }
+pub(super) fn parse() -> Args { Args::parse() }
+
+/// Synthesize any command line options with configuration file options.
+pub(crate) fn update(config: &mut Config, args: &Args) -> Result<()> {
+	// Indicate the admin console should be spawned automatically if the
+	// configuration file hasn't already.
+	config.admin_console_automatic |= args.console.unwrap_or(false);
+
+	Ok(())
+}
diff --git a/src/main/main.rs b/src/main/main.rs
index 959e86100..b13e117db 100644
--- a/src/main/main.rs
+++ b/src/main/main.rs
@@ -33,7 +33,7 @@ fn main() -> Result<(), Error> {
 		.build()
 		.expect("built runtime");
 
-	let server: Arc<Server> = Server::build(args, Some(runtime.handle()))?;
+	let server: Arc<Server> = Server::build(&args, Some(runtime.handle()))?;
 	runtime.spawn(signal::signal(server.clone()));
 	runtime.block_on(async_main(&server))?;
 
diff --git a/src/main/server.rs b/src/main/server.rs
index 73c06f0ca..b1cd6936e 100644
--- a/src/main/server.rs
+++ b/src/main/server.rs
@@ -21,8 +21,9 @@ pub(crate) struct Server {
 }
 
 impl Server {
-	pub(crate) fn build(args: Args, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> {
-		let config = Config::new(args.config)?;
+	pub(crate) fn build(args: &Args, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> {
+		let mut config = Config::new(&args.config)?;
+		crate::clap::update(&mut config, args)?;
 
 		#[cfg(feature = "sentry_telemetry")]
 		let sentry_guard = crate::sentry::init(&config);
-- 
GitLab