forked from gkaklas/labadoor
main: Add cli arguments as a configuration source
This commit is contained in:
parent
e7c34865c5
commit
eebb4ca762
3 changed files with 26 additions and 12 deletions
|
@ -13,6 +13,7 @@ labadoor-auth = { path = "../labadoor-auth", optional = true }
|
|||
labadoor-acl = { path = "../labadoor-acl" }
|
||||
serde = { version = "1.0.164", features = ["derive"] }
|
||||
config = "0.13.3"
|
||||
toml = "0.8.8"
|
||||
|
||||
[features]
|
||||
telegram = ["dep:labadoor-telegram"]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use clap::Parser;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct Cli {
|
||||
|
@ -20,14 +20,14 @@ pub enum Command {
|
|||
}
|
||||
|
||||
#[cfg(feature = "telegram")]
|
||||
#[derive(Deserialize, Parser, Debug)]
|
||||
#[derive(Serialize, Deserialize, Parser, Debug)]
|
||||
pub struct Telegram {
|
||||
#[clap(short, long)]
|
||||
pub token: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "matrix")]
|
||||
#[derive(Deserialize, Parser, Debug)]
|
||||
#[derive(Serialize, Deserialize, Parser, Debug)]
|
||||
pub struct Matrix {
|
||||
#[clap(short, long)]
|
||||
pub username: Option<String>,
|
||||
|
@ -38,7 +38,7 @@ pub struct Matrix {
|
|||
}
|
||||
|
||||
#[cfg(feature = "gpio")]
|
||||
#[derive(Deserialize, Parser, Debug)]
|
||||
#[derive(Serialize, Deserialize, Parser, Debug)]
|
||||
pub struct GPIO {
|
||||
#[clap(short, long)]
|
||||
pub device: Option<String>,
|
||||
|
|
|
@ -2,32 +2,45 @@ mod cli;
|
|||
mod to_config;
|
||||
use to_config::ToConfig;
|
||||
|
||||
macro_rules! add_cliargs {
|
||||
($d:ident,$section:expr,$i:ident) => {
|
||||
$d.add_source(config::File::from_str(
|
||||
&format!("[{}]\n{}", $section, toml::to_string($i).unwrap()),
|
||||
config::FileFormat::Toml,
|
||||
))
|
||||
.build()
|
||||
.unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = cli::parse();
|
||||
let config = config::Config::builder()
|
||||
.add_source(config::File::with_name("./config.toml").required(false))
|
||||
.add_source(config::Environment::with_prefix("LABADOOR").separator("_"))
|
||||
.build()
|
||||
.unwrap();
|
||||
.add_source(config::File::with_name(path).required(false))
|
||||
.add_source(config::Environment::with_prefix("LABADOOR").separator("_"));
|
||||
match &cli.command {
|
||||
#[cfg(feature = "telegram")]
|
||||
cli::Command::Telegram(_) => {
|
||||
cli::Command::Telegram(cliargs) => {
|
||||
let config = add_cliargs!(config, "telegram", cliargs);
|
||||
let telegram = config.get::<cli::Telegram>("telegram").unwrap().to_config();
|
||||
labadoor_telegram::telegram(telegram);
|
||||
}
|
||||
#[cfg(feature = "matrix")]
|
||||
cli::Command::Matrix(_) => {
|
||||
cli::Command::Matrix(cliargs) => {
|
||||
let config = add_cliargs!(config, "matrix", cliargs);
|
||||
let matrix = config.get::<cli::Matrix>("matrix").unwrap().to_config();
|
||||
labadoor_matrix::matrix(matrix);
|
||||
}
|
||||
#[cfg(feature = "gpio")]
|
||||
cli::Command::GPIO(_) => {
|
||||
cli::Command::GPIO(cliargs) => {
|
||||
let config = add_cliargs!(config, "gpio", cliargs);
|
||||
let gpio = config.get::<cli::GPIO>("gpio").unwrap().to_config();
|
||||
labadoor_gpio::gpio(gpio);
|
||||
}
|
||||
#[cfg(feature = "auth")]
|
||||
cli::Command::Auth(cli) => {
|
||||
labadoor_auth::auth(cli, config);
|
||||
let config = config.build().unwrap();
|
||||
labadoor_auth::auth(&cli, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue