use anyhow::Result;
use std::io::Write;
use std::process::{Command, Stdio};
pub fn authenticate_sudo(password: &str) -> Result<bool> {
let mut child = Command::new("sudo")
.arg("-S")
.arg("-v")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
if let Some(mut stdin) = child.stdin.take() {
writeln!(stdin, "{}", password)?;
}
let status = child.wait()?;
Ok(status.success())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(unix)]
fn authenticate_sudo_with_wrong_password_does_not_panic() {
let _ = authenticate_sudo("definitely-not-the-real-password-12345");
}
}