mirror of
https://codeberg.org/icewind/tasmota-backup.git
synced 2026-06-03 06:14:12 +02:00
fix mqtt password file
This commit is contained in:
parent
6081cf31ec
commit
62c663bf9a
3 changed files with 26 additions and 22 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -6,4 +6,5 @@ result
|
|||
*.snap.new
|
||||
*.dmp
|
||||
config.toml
|
||||
output
|
||||
output
|
||||
passwordfile
|
||||
|
|
@ -2,6 +2,7 @@ use anyhow::{Context, Result};
|
|||
use serde::Deserialize;
|
||||
use std::fs::read_to_string;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tasmota_mqtt_client::TasmotaClient;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
|
|
@ -36,7 +37,8 @@ pub struct MqttConfig {
|
|||
#[serde(default = "default_port")]
|
||||
pub port: u16,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
#[serde(flatten)]
|
||||
pub password: Option<PasswordConfig>,
|
||||
}
|
||||
|
||||
fn default_port() -> u16 {
|
||||
|
|
@ -44,18 +46,20 @@ fn default_port() -> u16 {
|
|||
}
|
||||
|
||||
impl MqttConfig {
|
||||
pub fn credentials(&self) -> Option<(&str, &str)> {
|
||||
self.username.as_deref().zip(self.password.as_deref())
|
||||
}
|
||||
}
|
||||
pub async fn connect(&self) -> Result<TasmotaClient> {
|
||||
let password = self
|
||||
.password
|
||||
.as_ref()
|
||||
.map(|password| password.get())
|
||||
.transpose()?;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RawMqttConfig {
|
||||
pub hostname: String,
|
||||
pub port: u16,
|
||||
pub username: Option<String>,
|
||||
#[serde(flatten)]
|
||||
pub password: Option<PasswordConfig>,
|
||||
Ok(TasmotaClient::connect(
|
||||
&self.hostname,
|
||||
self.port,
|
||||
self.username.as_deref().zip(password.as_deref()),
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
@ -81,8 +85,12 @@ impl PasswordConfig {
|
|||
pub fn get(&self) -> Result<String> {
|
||||
match self {
|
||||
PasswordConfig::Raw { password } => Ok(password.clone()),
|
||||
PasswordConfig::File { password_file } => Ok(read_to_string(password_file)
|
||||
.with_context(|| format!("Failed to read password from {password_file}"))?),
|
||||
PasswordConfig::File { 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,12 +25,7 @@ async fn main() -> Result<()> {
|
|||
let config = Config::load(&args.config)?;
|
||||
let device_password = config.device.password.get()?;
|
||||
|
||||
let client = TasmotaClient::connect(
|
||||
&config.mqtt.hostname,
|
||||
config.mqtt.port,
|
||||
config.mqtt.credentials(),
|
||||
)
|
||||
.await?;
|
||||
let client = config.mqtt.connect().await?;
|
||||
|
||||
info!("waiting for device discovery");
|
||||
|
||||
|
|
@ -62,7 +57,7 @@ async fn download(
|
|||
target_dir: &Path,
|
||||
device_password: &str,
|
||||
) -> 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 existing_hash = target_path
|
||||
.exists()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue