Added clap and experimenting with that framework
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Martin Slot 2022-09-17 09:24:30 +02:00
parent af95f5d1f1
commit 9d41845f0c
5 changed files with 26 additions and 21 deletions

View File

@ -10,3 +10,4 @@ refinery = { version = "0.8",features = ["rusqlite"]}
rusqlite = { version = "0.27.0"}
config = { version = "0.13.1", features = ["toml"] }
serde = { version = "1.0", features = ["derive"]}
clap = { version = "3.2.21", features = ["derive"] }

View File

@ -1,7 +1,13 @@
use std::env;
use clap::Parser;
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
use std::fmt::Formatter;
use std::str::FromStr;
use std::{env, fmt};
pub struct ArgumentParser {}
impl ArgumentParser {}
#[derive(Debug, Deserialize)]
pub struct Database {
@ -18,21 +24,12 @@ impl Settings {
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("settings"))
// Add in the current environment file
// Default to 'development' env
// Note that this file is _optional_
.add_source(File::with_name(&format!("settings.{}", run_mode)).required(false))
// Add in a local configuration file
// This file shouldn't be checked in to git
.add_source(File::with_name("local").required(false))
// Eg.. `VERSION_CHECKER_DEBUG=1 ./target/version_checker` would set the `debug` key
.add_source(Environment::with_prefix("version_checker"))
.build()?;
// Now that we're done, let's access our configuration
s.try_deserialize()
}
}

View File

@ -1,5 +1,7 @@
use configuration::ArgumentParser;
use configuration::Settings;
use rusqlite::Connection;
pub mod settings;
pub mod configuration;
mod embedded {
use refinery::embed_migrations;
@ -11,11 +13,16 @@ pub fn migrate() {
embedded::migrations::runner().run(&mut conn).unwrap();
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
struct Application {
settings: Settings,
}
impl Application {
pub fn new(settings: Settings, arguments: ArgumentParser) -> Application {
Application { settings }
}
pub fn run(&self) {
migrate();
}
}

View File

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
application = { path= "../application" }
application = { path= "../application" }

View File

@ -1,10 +1,10 @@
#[test]
fn can_default_settings_be_loaded() {
let settings = application::settings::Settings::new().expect("error when loading settings");
let settings = application::configuration::Settings::new().expect("error when loading settings");
}
#[test]
fn database_settings_can_be_loaded() {
let settings = application::settings::Settings::new().expect("error when loading settings");
let settings = application::configuration::Settings::new().expect("error when loading settings");
assert_eq!("version_checker_db", settings.database.connection_string);
}