fix mqtt password file

This commit is contained in:
Robin Appelman 2024-01-27 18:48:21 +01:00
commit 62c663bf9a
3 changed files with 26 additions and 22 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ result
*.dmp *.dmp
config.toml config.toml
output output
passwordfile

View file

@ -2,6 +2,7 @@ use anyhow::{Context, Result};
use serde::Deserialize; use serde::Deserialize;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use tasmota_mqtt_client::TasmotaClient;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Config { pub struct Config {
@ -36,7 +37,8 @@ pub struct MqttConfig {
#[serde(default = "default_port")] #[serde(default = "default_port")]
pub port: u16, pub port: u16,
pub username: Option<String>, pub username: Option<String>,
pub password: Option<String>, #[serde(flatten)]
pub password: Option<PasswordConfig>,
} }
fn default_port() -> u16 { fn default_port() -> u16 {
@ -44,18 +46,20 @@ fn default_port() -> u16 {
} }
impl MqttConfig { impl MqttConfig {
pub fn credentials(&self) -> Option<(&str, &str)> { pub async fn connect(&self) -> Result<TasmotaClient> {
self.username.as_deref().zip(self.password.as_deref()) let password = self
} .password
} .as_ref()
.map(|password| password.get())
.transpose()?;
#[derive(Debug, Deserialize)] Ok(TasmotaClient::connect(
pub struct RawMqttConfig { &self.hostname,
pub hostname: String, self.port,
pub port: u16, self.username.as_deref().zip(password.as_deref()),
pub username: Option<String>, )
#[serde(flatten)] .await?)
pub password: Option<PasswordConfig>, }
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -81,8 +85,12 @@ impl PasswordConfig {
pub fn get(&self) -> Result<String> { pub fn get(&self) -> Result<String> {
match self { match self {
PasswordConfig::Raw { password } => Ok(password.clone()), PasswordConfig::Raw { password } => Ok(password.clone()),
PasswordConfig::File { password_file } => Ok(read_to_string(password_file) PasswordConfig::File { password_file } => {
.with_context(|| format!("Failed to read password from {password_file}"))?), let mut content = read_to_string(password_file)
.with_context(|| format!("Failed to read password from {password_file}"))?;
content.truncate(content.trim_end().len());
Ok(content)
}
} }
} }
} }

View file

@ -25,12 +25,7 @@ async fn main() -> Result<()> {
let config = Config::load(&args.config)?; let config = Config::load(&args.config)?;
let device_password = config.device.password.get()?; let device_password = config.device.password.get()?;
let client = TasmotaClient::connect( let client = config.mqtt.connect().await?;
&config.mqtt.hostname,
config.mqtt.port,
config.mqtt.credentials(),
)
.await?;
info!("waiting for device discovery"); info!("waiting for device discovery");
@ -62,7 +57,7 @@ async fn download(
target_dir: &Path, target_dir: &Path,
device_password: &str, device_password: &str,
) -> Result<()> { ) -> Result<()> {
let file = client.download_config(&device, &device_password).await?; let file = client.download_config(device, device_password).await?;
let target_path = target_dir.join(&file.name); let target_path = target_dir.join(&file.name);
let existing_hash = target_path let existing_hash = target_path
.exists() .exists()