From c758eed8208a92d02d3ae3e8dc500d5fad5ea4e4 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sat, 13 Nov 2021 15:29:48 +0100 Subject: [PATCH] Require -c config file command-line argument. --- client/config.toml | 2 ++ client/src/main.rs | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 client/config.toml diff --git a/client/config.toml b/client/config.toml new file mode 100644 index 0000000..7b5797a --- /dev/null +++ b/client/config.toml @@ -0,0 +1,2 @@ +user_name = "solstice" +password = "topsekrit" diff --git a/client/src/main.rs b/client/src/main.rs index ccdfb5a..4cda8d7 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use anyhow::Context; use clap::{App, Arg}; use crossbeam_channel; @@ -20,16 +22,15 @@ mod room; mod testing; mod user; -use config::Config; +use config::{Config, TomlConfig}; use context::{ContextBundle, ContextOptions}; use dispatcher::Dispatcher; -fn old_main() { +fn old_main(config: Config) { let (request_tx, _request_rx) = crossbeam_channel::bounded(100); let (_response_tx, response_rx) = crossbeam_channel::bounded(100); - let mut client = - client::Client::new(request_tx, response_rx, Config::default()); + let mut client = client::Client::new(request_tx, response_rx, config); client.run(); } @@ -77,9 +78,7 @@ async fn run_client( Ok(()) } -async fn async_main() -> anyhow::Result<()> { - let config = Config::default(); - +async fn async_main(config: Config) -> anyhow::Result<()> { let mut options = ContextOptions::default(); options.initial_state.user_name = config.credentials.user_name().to_string(); @@ -142,15 +141,29 @@ async fn main() -> anyhow::Result<()> { .long("sync") .help("Use the old synchronous engine."), ) + .arg( + Arg::with_name("config") + .short("c") + .long("config") + .value_name("FILE") + .required(true) + .help("Path to the config file to use."), + ) .get_matches(); + let path = Path::new(matches.value_of("config").unwrap()); + let toml_config = TomlConfig::from_file_path(path).await?; + let config = Config::from_toml(toml_config)?; + if matches.is_present("sync") { info!("Running in synchronous mode."); - tokio::task::spawn_blocking(old_main) + tokio::task::spawn_blocking(|| old_main(config)) .await .context("running synchronous main") } else { info!("Running in asynchronous mode."); - async_main().await.context("running asynchronous main") + async_main(config) + .await + .context("running asynchronous main") } }