use std::{env, ffi::OsString, os::unix::process::CommandExt, process::Command};
use anyhow::{anyhow, Context, Result};
pub fn require_root(action: &str) -> Result<()> {
if unsafe { libc::geteuid() } == 0 {
return Ok(());
}
let exe = env::current_exe().context("locating linprov binary")?;
let argv: Vec<OsString> = env::args_os().skip(1).collect();
let err = Command::new("sudo").arg("--").arg(&exe).args(&argv).exec();
let argv_str: Vec<String> = argv
.iter()
.map(|s| s.to_string_lossy().into_owned())
.collect();
Err(anyhow!(
"`{action}` needs root, and re-executing under `sudo` to ask for \
a password failed: {err}.\n\
\n\
If your system uses a different escalation tool (`doas`, `su`, \
`pkexec`, ...), invoke it on this command directly:\n\
\n {} {}\n",
exe.display(),
argv_str.join(" "),
))
}