feat: use password file as primary source if provided

This commit is contained in:
rustmailer
2025-12-26 14:44:49 +08:00
parent 4af5176b65
commit 0f3ad83004
2 changed files with 15 additions and 10 deletions
+1
View File
@@ -133,6 +133,7 @@ pub struct Settings {
#[clap( #[clap(
long, long,
env, env,
default_value = "change-this-default-password-now",
help = "Set the encryption password for bichon. Alternatively, you can use --bichon-encrypt-password-file. If both are set, this parameter takes precedence over the file." help = "Set the encryption password for bichon. Alternatively, you can use --bichon-encrypt-password-file. If both are set, this parameter takes precedence over the file."
)] )]
pub bichon_encrypt_password: Option<String>, pub bichon_encrypt_password: Option<String>,
+12 -8
View File
@@ -29,16 +29,20 @@ use crate::modules::error::BichonResult;
use crate::modules::settings::cli::SETTINGS; use crate::modules::settings::cli::SETTINGS;
use crate::raise_error; use crate::raise_error;
static ENCRYPT_PASSWORD: LazyLock<String> = static ENCRYPT_PASSWORD: LazyLock<String> = LazyLock::new(|| {
LazyLock::new(|| match &SETTINGS.bichon_encrypt_password { if let Some(file_path) = &SETTINGS.bichon_encrypt_password_file {
Some(p) => p.clone(), return fs::read_to_string(file_path)
None => {
// unwrap() is safe here, because SETTINGS validates that at least one of the encrypt_password
// fields is set.
fs::read_to_string(SETTINGS.bichon_encrypt_password_file.as_ref().unwrap())
.expect("failed to read the file with the encrypt password") .expect("failed to read the file with the encrypt password")
.trim()
.to_string();
} }
});
if let Some(p) = &SETTINGS.bichon_encrypt_password {
return p.clone();
}
panic!("Neither encrypt_password nor encrypt_password_file is set. This should have been validated by SETTINGS.");
});
struct SingleNonceSequence([u8; 12]); struct SingleNonceSequence([u8; 12]);