hue-control/src/config/mod.rs
2022-04-09 22:51:17 +02:00

59 lines
1.2 KiB
Rust

mod config_file;
mod environment;
use eyre::{eyre, Result};
#[derive(Debug)]
pub struct Config {
pub hostname: String,
pub token: String,
}
pub async fn resolve() -> Result<Config> {
let mut config = Config {
hostname: "".to_string(),
token: "".to_string(),
};
let config_file = config_file::read().await?;
if let Some(c) = config_file {
if let Some(token) = c.token {
config.token = token;
}
if let Some(host) = c.host {
config.hostname = host.hostname
}
}
if let Ok(token) = std::env::var("HUE_TOKEN") {
config.token = token;
}
if let Ok(host) = std::env::var("HUE_HOSTNAME") {
config.hostname = host;
}
println!("{:?}", config);
if let Err(e) = validate_config(&config) {
return Err(e);
}
Ok(config)
}
fn validate_config(config: &Config) -> Result<()> {
if config.token.is_empty() {
return Err(eyre!("Token config variable is empty"));
}
if config.token.len() != 40 || !config.token.is_ascii() {
return Err(eyre!("Token format is invalid"));
}
if config.hostname.is_empty() {
return Err(eyre!("hostname is empty"));
}
Ok(())
}