Skip to content

Commit

Permalink
Create new ./src/lib files for new/old
Browse files Browse the repository at this point in the history
  • Loading branch information
BanjoFox committed Aug 22, 2023
1 parent 41dd1b4 commit 4c7e0a7
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 36 deletions.
138 changes: 138 additions & 0 deletions src/lib-new.broken.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::env;
use anyhow::{Context, Result};
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use config::{Config, Environment, File, FileFormat};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short = 'c', long, help = "Sets a custom config file")]
config: Option<std::path::PathBuf>,

#[arg(short = 'l', long, help = "Sets logging destination")]
log: Option<std::path::PathBuf>,

#[command(flatten)]
verbose: Verbosity,
}

pub fn configure(parsed_args: Args) -> Result<Config> {
// Set defaults
let mut builder = Config::builder();
builder = builder
.set_default("cfg_file", concat!(env!("CARGO_PKG_NAME"), ".toml"))
.context(ErrorKind::ConfigImmutable)?;
builder = builder
.set_default("Log.file", "_CONSOLE_")
.context(ErrorKind::ConfigImmutable)?;
builder = builder
.set_default("Web.address", "127.0.0.1")
.context(ErrorKind::ConfigImmutable)?;
builder = builder
.set_default("Web.port", 7878)
.context(ErrorKind::ConfigImmutable)?;

// Determine config file
let config_path = parsed_args.config.unwrap();
let config_file = config_path.to_str().unwrap();

builder = builder
.set_override("cfg_file", config_file)
.context(ErrorKind::ConfigImmutable)?;

builder = builder.add_source(File::new(config_file, FileFormat::Toml)); // Or whatever format

builder = builder
.set_override("cfg_file", config_file)
.context(ErrorKind::ConfigImmutable)?;

// Apply environmenet variable overrides
let env_vars = Environment::with_prefix("AARDWOLF")
.separator("_")
.ignore_empty(true);
builder = builder.add_source(env_vars);

let log_path = parsed_args.log.unwrap();
builder = builder
.set_override("Log.file", log_path.to_str())
.context(ErrorKind::ConfigImmutable)?;

match builder.build() {
Ok(config) => {
env::set_var("DATABASE_URL", db_conn_string(&config)?);
return Ok(config)
},
Err(e) => {
// Throw an error
return Err(e.into());
}
}
}

pub fn db_conn_string(config: &Config) -> Result<String> {
let keys = [
"Database.type",
"Database.username",
"Database.password",
"Database.host",
"Database.port",
"Database.database",
];

let string_vec: Vec<String> = keys
.iter()
.map(|key| config.get_string(key))
.collect::<Result<_, _>>()
.context(ErrorKind::ConfigMissingKeys)?;

match string_vec[0].as_str()
{
"postgres" | "postgresql" => (),
_ => Err(ErrorKind::UnsupportedDbScheme)?,
}

Ok(format!(
"{scheme}://{username}:{password}@{host}:{port}/{database}",
scheme = string_vec[0],
username = string_vec[1],
password = string_vec[2],
host = string_vec[3],
port = string_vec[4],
database = string_vec[5],
))
}

#[derive(Debug, thiserror::Error)]
#[error("Configuration was missing expected keys: [{:?}]", _0)]
pub struct MissingKeys(Vec<String>);

#[derive(Clone, Copy, Debug, Eq, thiserror::Error, Hash, PartialEq)]
pub enum ErrorKind {
#[error("Unsupported database scheme, only 'postgres' and 'postgresql' are allowed.")]
UnsupportedDbScheme,
#[error("Configuration was missing expected keys")]
ConfigMissingKeys,
#[error("Config struct cannot be modified")]
ConfigImmutable,
}

#[cfg(feature = "simple-logging")]
pub fn begin_log(config: &config::Config) {
use log::LevelFilter;

match config.get_string("Log.file").unwrap().as_ref() {
"_CONSOLE_" => (),
l => simple_logging::log_to_file(l, LevelFilter::Debug).unwrap(),
}
}

#[cfg(feature = "syslog")]
pub fn begin_log(config: &config::Config) {
// TODO: Implement log-syslog:begin_log()
}

#[cfg(feature = "systemd")]
pub fn begin_log(config: &config::Config) {
// TODO: Implement use-systemd:begin_log()
}
71 changes: 35 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;
use anyhow::{Context, Result};
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use config::{Config, ConfigError, Environment};
use config::{Config, Environment, File, FileFormat};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -19,56 +19,55 @@ pub struct Args {

pub fn configure(parsed_args: Args) -> Result<Config> {
// Set defaults
let mut config = Config::default();
config
.set_default::<&str>("cfg_file", concat!(env!("CARGO_PKG_NAME"), ".toml"))
let mut builder = Config::builder();
builder = builder
.set_default("cfg_file", concat!(env!("CARGO_PKG_NAME"), ".toml"))
.context(ErrorKind::ConfigImmutable)?;
config
.set_default::<&str>("Log.file", "_CONSOLE_")
builder = builder
.set_default("Log.file", "_CONSOLE_")
.context(ErrorKind::ConfigImmutable)?;
config
.set_default::<&str>("Web.address", "127.0.0.1")
builder = builder
.set_default("Web.address", "127.0.0.1")
.context(ErrorKind::ConfigImmutable)?;
config
builder = builder
.set_default("Web.port", 7878)
.context(ErrorKind::ConfigImmutable)?;

// Determine config file
if let Ok(config_file) = env::var("AARDWOLF_CONFIG") {
config
.set("cfg_file", config_file)
.context(ErrorKind::ConfigImmutable)?;
}
let config_path = parsed_args.config.unwrap();
let config_file = config_path.to_str().unwrap();

if let Some(config_path) = parsed_args.config {
config
.set("cfg_file", config_path.to_str())
.context(ErrorKind::ConfigImmutable)?;
}
builder = builder
.set_override("cfg_file", config_file)
.context(ErrorKind::ConfigImmutable)?;

// Merge config file and apply overrides
let config_file_string = config
.get_string("cfg_file")
.context(ErrorKind::ConfigMissingKeys)?;
let config_file = config::File::with_name(&config_file_string);
config.merge(config_file).context(ErrorKind::ConfigImmutable)?;
builder = builder.add_source(File::new(config_file, FileFormat::Toml)); // Or whatever format

// Apply environment variable overrides
builder = builder
.set_override("cfg_file", config_file)
.context(ErrorKind::ConfigImmutable)?;

// Apply environmenet variable overrides
let env_vars = Environment::with_prefix("AARDWOLF")
.separator("_")
.ignore_empty(true);
config.merge(env_vars).context(ErrorKind::ConfigImmutable)?;

// Remove the need for a .env file to avoid defining env vars twice.
env::set_var("DATABASE_URL", db_conn_string(&config)?);
builder = builder.add_source(env_vars);

if let Some(log_path) = parsed_args.log {
config
.set("Log.file", log_path.to_str())
.context(ErrorKind::ConfigImmutable)?;
let log_path = parsed_args.log.unwrap();
builder = builder
.set_override("Log.file", log_path.to_str())
.context(ErrorKind::ConfigImmutable)?;

match builder.build() {
Ok(config) => {
env::set_var("DATABASE_URL", db_conn_string(&config)?);
return Ok(config)
},
Err(e) => {
// Throw an error
return Err(e.into());
}
}

Ok(config)
}

pub fn db_conn_string(config: &Config) -> Result<String> {
Expand Down

0 comments on commit 4c7e0a7

Please sign in to comment.