use std::fs;
use secrecy::SecretString;
pub fn get_passphrase(passphrase_path: Option<String>) -> anyhow::Result<SecretString> {
fn get_passphrase_from_file_if_exists(path: &str) -> Option<SecretString> {
let contents = fs::read_to_string(path);
match contents {
Ok(contents) => Some(SecretString::new(Box::from(contents))),
Err(_) => None
}
}
if let Some(passphrase_path) = passphrase_path {
let passphrase = get_passphrase_from_file_if_exists(&passphrase_path);
match passphrase {
Some(passphrase) => return Ok(passphrase),
None => anyhow::bail!("no passphrase found at {}", passphrase_path)
}
}
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /run/secrets/hashiverse_passphrase") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /run/secrets/HASHIVERSE_PASSPHRASE") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /mnt/secrets/hashiverse_passphrase") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /mnt/secrets/HASHIVERSE_PASSPHRASE") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /etc/secrets/hashiverse_passphrase") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" /etc/secrets/HASHIVERSE_PASSPHRASE") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" ./.hashiverse_passphrase") { return Ok(passphrase); }
if let Some(passphrase) = get_passphrase_from_file_if_exists(" ./.HASHIVERSE_PASSPHRASE") { return Ok(passphrase); }
if let Ok(passphrase) = std::env::var("HASHIVERSE_PASSPHRASE") { return Ok(SecretString::new(Box::from(passphrase))); }
anyhow::bail!("no passphrase found - please (at worst) set the HASHIVERSE_PASSPHRASE environment variable to something memorable");
}