#[cfg(windows)]
pub fn read_hklm_value(subkey: &str, value: &str) -> Option<String> {
use winreg::RegKey;
use winreg::enums::HKEY_LOCAL_MACHINE;
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let key = hklm.open_subkey(subkey).ok()?;
let s: String = key.get_value(value).ok()?;
if s.is_empty() { None } else { Some(s) }
}
#[cfg(not(windows))]
pub fn read_hklm_value(_subkey: &str, _value: &str) -> Option<String> {
None
}
#[cfg(windows)]
pub fn write_hklm_value(subkey: &str, value: &str, data: &str) -> Result<(), String> {
use winreg::RegKey;
use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_WRITE};
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let key = hklm
.open_subkey_with_flags(subkey, KEY_WRITE)
.map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => format!(
"HKLM\\{subkey} is not present — refusing to create it. A key created here \
would not carry the SYSTEM+Administrators ACL the deploy scripts apply, \
leaving the value readable by any logged-in user. Deploy this host first."
),
std::io::ErrorKind::PermissionDenied => format!(
"HKLM\\{subkey} exists but could not be opened for writing: {e}. It is \
restricted to SYSTEM + Administrators — run this elevated."
),
_ => format!("HKLM\\{subkey} could not be opened for writing: {e}"),
})?;
key.set_value(value, &data.to_string())
.map_err(|e| format!("writing HKLM\\{subkey}\\{value}: {e}"))
}
#[cfg(not(windows))]
pub fn write_hklm_value(_subkey: &str, _value: &str, _data: &str) -> Result<(), String> {
Err("registry secrets are Windows-only".to_string())
}