mirror of
https://github.com/davegallant/rfd-notify.git
synced 2025-08-06 08:43:39 +00:00
@@ -1,40 +1,76 @@
|
||||
extern crate toml;
|
||||
use serde_derive::Deserialize;
|
||||
extern crate envconfig;
|
||||
extern crate envconfig_derive;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use envconfig::Envconfig;
|
||||
use std::fs;
|
||||
use std::vec::Vec;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate serial_test;
|
||||
#[cfg(test)]
|
||||
use serial_test::serial;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_config() {
|
||||
let file = "./examples/config.toml";
|
||||
parse(&file);
|
||||
#[should_panic]
|
||||
#[serial]
|
||||
fn load_config_with_missing_sendgrid_api_key() {
|
||||
std::env::remove_var("SENDGRID_API_KEY");
|
||||
let file = "./examples/config.yaml";
|
||||
load(&file);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn load_config() {
|
||||
let file = "./examples/config.yaml";
|
||||
std::env::set_var("SENDGRID_API_KEY", "FAKE");
|
||||
std::env::set_var("SENDGRID_MAIL_FROM", "notify@rfd-notify.org");
|
||||
std::env::set_var("SENDGRID_MAIL_TO", "test@email.com");
|
||||
load(&file);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub expressions: Vec<String>,
|
||||
pub sendgrid: Sendgrid,
|
||||
pub sendgrid: SendgridConfig,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Sendgrid {
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
pub struct ConfigFile {
|
||||
pub expressions: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Envconfig, Debug)]
|
||||
pub struct SendgridConfig {
|
||||
#[envconfig(from = "SENDGRID_MAIL_FROM")]
|
||||
pub mail_from: String,
|
||||
|
||||
#[envconfig(from = "SENDGRID_MAIL_TO")]
|
||||
pub mail_to: String,
|
||||
|
||||
#[envconfig(from = "SENDGRID_API_KEY")]
|
||||
pub api_key: String,
|
||||
}
|
||||
|
||||
pub fn parse(filename: &str) -> Config {
|
||||
pub fn load(filename: &str) -> Config {
|
||||
// Initialize expressions from file
|
||||
let contents = fs::read_to_string(filename)
|
||||
.unwrap_or_else(|e| panic!("Unable to read configuration file '{}'. {}", filename, e));
|
||||
let config: Config = toml::from_str(&contents).unwrap_or_else(|e| {
|
||||
panic!(
|
||||
"Unable to parse configuration with contents: {}. {}",
|
||||
contents, e
|
||||
)
|
||||
});
|
||||
|
||||
config
|
||||
let config_file: ConfigFile = serde_yaml::from_str(&contents).unwrap();
|
||||
|
||||
// Initialize config from environment variables or terminate the process.
|
||||
let sendgrid_config = SendgridConfig::init_from_env().unwrap();
|
||||
|
||||
Config {
|
||||
expressions: config_file.expressions,
|
||||
sendgrid: sendgrid_config,
|
||||
}
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ fn main() {
|
||||
let matches = app.get_matches();
|
||||
|
||||
let config = matches.value_of("config").unwrap();
|
||||
let parsed_config = config::parse(config);
|
||||
let parsed_config = config::load(config);
|
||||
|
||||
info!("{:?}\n", parsed_config);
|
||||
let hot_deals = rfd::get_hot_deals().map_err(|err| error!("{:?}", err)).ok();
|
||||
|
Reference in New Issue
Block a user