Read authentication tokens for bots from file and environment variables

This commit is contained in:
George Kaklamanos 2023-06-30 20:19:29 +03:00
parent ad7074ad03
commit a07ff25a1e
4 changed files with 20 additions and 4 deletions

View file

@ -9,6 +9,8 @@ labadoor-telegram = { path = "../labadoor-telegram", optional = true }
labadoor-matrix = { path = "../labadoor-matrix", optional = true } labadoor-matrix = { path = "../labadoor-matrix", optional = true }
labadoor-csv = { path = "../labadoor-csv", optional = true } labadoor-csv = { path = "../labadoor-csv", optional = true }
labadoor-gpio = { path = "../labadoor-gpio", optional = true } labadoor-gpio = { path = "../labadoor-gpio", optional = true }
serde = { version = "1.0.164", features = ["derive"] }
config = "0.13.3"
[features] [features]
telegram = [ "dep:labadoor-telegram" ] telegram = [ "dep:labadoor-telegram" ]

6
labadoor/config.toml Normal file
View file

@ -0,0 +1,6 @@
[telegram]
token = ""
[matrix]
username = ""
password = ""

View file

@ -1,4 +1,5 @@
use clap::Parser; use clap::Parser;
use serde::Deserialize;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct Cli { pub struct Cli {
@ -19,14 +20,14 @@ pub enum Command {
} }
#[cfg(feature = "telegram")] #[cfg(feature = "telegram")]
#[derive(Parser, Debug)] #[derive(Deserialize, Parser, Debug)]
pub struct Telegram { pub struct Telegram {
#[clap(short, long)] #[clap(short, long)]
pub token: Option<String>, pub token: Option<String>,
} }
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
#[derive(Parser, Debug)] #[derive(Deserialize, Parser, Debug)]
pub struct Matrix { pub struct Matrix {
#[clap(short, long)] #[clap(short, long)]
pub username: Option<String>, pub username: Option<String>,

View file

@ -2,14 +2,21 @@ mod cli;
fn main() { fn main() {
let cli = cli::parse(); 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();
match &cli.command { match &cli.command {
#[cfg(feature = "telegram")] #[cfg(feature = "telegram")]
cli::Command::Telegram(_) => { cli::Command::Telegram(_) => {
labadoor_telegram::telegram(); let telegram = config.get::<cli::Telegram>("telegram").unwrap();
labadoor_telegram::telegram(telegram.token.unwrap());
} }
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
cli::Command::Matrix(_) => { cli::Command::Matrix(_) => {
labadoor_matrix::matrix(); let matrix = config.get::<cli::Matrix>("matrix").unwrap();
labadoor_matrix::matrix(matrix.username.unwrap(), matrix.password.unwrap());
} }
#[cfg(feature = "csv")] #[cfg(feature = "csv")]
cli::Command::CSV => { cli::Command::CSV => {