Add a hostname and token cli param

This commit is contained in:
Daan Boerlage 2022-04-09 23:14:26 +02:00
parent cdfb24c487
commit fa53ee6f60
Signed by: daan
GPG key ID: FCE070E1E4956606
4 changed files with 27 additions and 5 deletions

View file

@ -5,6 +5,12 @@ use std::str::FromStr;
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)] #[clap(propagate_version = true)]
pub struct Args { pub struct Args {
#[clap(short, long)]
pub token: Option<String>,
#[clap(short, long)]
pub hostname: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
pub command: Commands, pub command: Commands,
} }

View file

@ -1 +0,0 @@

View file

@ -1,5 +1,4 @@
mod config_file; mod config_file;
mod environment;
use eyre::{eyre, Result}; use eyre::{eyre, Result};
@ -9,12 +8,15 @@ pub struct Config {
pub token: String, pub token: String,
} }
pub async fn resolve() -> Result<Config> { pub async fn resolve(args: &crate::args::Args) -> Result<Config> {
let mut config = Config { let mut config = Config {
hostname: "".to_string(), hostname: "".to_string(),
token: "".to_string(), token: "".to_string(),
}; };
///////
// Config file
///////
let config_file = config_file::read().await?; let config_file = config_file::read().await?;
if let Some(c) = config_file { if let Some(c) = config_file {
if let Some(token) = c.token { if let Some(token) = c.token {
@ -25,6 +27,9 @@ pub async fn resolve() -> Result<Config> {
} }
} }
///////
// Environment variables
///////
if let Ok(token) = std::env::var("HUE_TOKEN") { if let Ok(token) = std::env::var("HUE_TOKEN") {
config.token = token; config.token = token;
} }
@ -33,8 +38,20 @@ pub async fn resolve() -> Result<Config> {
config.hostname = host; config.hostname = host;
} }
println!("{:?}", config); ///////
// CLI Params
///////
if let Some(token) = &args.token {
config.token = token.clone()
}
if let Some(hostname) = &args.hostname {
config.hostname = hostname.clone()
}
///////
// Validate config
///////
if let Err(e) = validate_config(&config) { if let Err(e) = validate_config(&config) {
return Err(e); return Err(e);
} }

View file

@ -10,7 +10,7 @@ use eyre::{eyre, Result};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let args = args::Args::parse(); let args = args::Args::parse();
let config = config::resolve().await?; let config = config::resolve(&args).await?;
let res = match &args.command { let res = match &args.command {
args::Commands::Toggle { id, state } => { args::Commands::Toggle { id, state } => {