From 8643faf519716e810518385770b18b1a43697608 Mon Sep 17 00:00:00 2001 From: "Arnaud (Arhuman) ASSAD" Date: Mon, 16 May 2022 06:16:09 +0200 Subject: [PATCH] Split files add carman module and settings file --- src/{lib.rs => arguments.rs} | 0 src/{main.rs => carman.rs} | 4 ++- src/settings.rs | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) rename src/{lib.rs => arguments.rs} (100%) rename src/{main.rs => carman.rs} (84%) create mode 100644 src/settings.rs diff --git a/src/lib.rs b/src/arguments.rs similarity index 100% rename from src/lib.rs rename to src/arguments.rs diff --git a/src/main.rs b/src/carman.rs similarity index 84% rename from src/main.rs rename to src/carman.rs index 3c91870..a716244 100644 --- a/src/main.rs +++ b/src/carman.rs @@ -1,5 +1,7 @@ -use carman::Arguments; +mod carman; use clap::Parser; +use carman::Arguments; +use carman::Settings; /// Carman code deployment Daemon diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000..6248590 --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,53 @@ +use config::{Config, ConfigError, Environment, File}; +use serde_derive::Deserialize; +use std::env; + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Repository { + key: String, + token: String, + url: String, + version: u8, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub struct Settings { + debug: bool, + log: u8, + repository: Repository, +} + +impl Settings { + pub fn new() -> Result { + let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into()); + + let s = Config::builder() + // Start off by merging in the "default" configuration file + .add_source(File::with_name("examples/hierarchical-env/config/default")) + // Add in the current environment file + // Default to 'development' env + // Note that this file is _optional_ + .add_source( + File::with_name(&format!("examples/hierarchical-env/config/{}", run_mode)) + .required(false), + ) + // Add in a local configuration file + // This file shouldn't be checked in to git + .add_source(File::with_name("examples/hierarchical-env/config/local").required(false)) + // Add in settings from the environment (with a prefix of APP) + // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key + .add_source(Environment::with_prefix("carman")) + // You may also programmatically change settings + .set_override("database.url", "postgres://")? + .build()?; + + // Now that we're done, let's access our configuration + println!("debug: {:?}", s.get_bool("debug")); + println!("database: {:?}", s.get::("database.url")); + + // You can deserialize (and thus freeze) the entire configuration as + s.try_deserialize() + } +}