Execute-elevated (#892)

* Introduce the execute elevated method (fix #885)

* fmt

* Fix nix with doas

* Bad import
This commit is contained in:
Roey Darwish Dror
2022-04-09 16:29:55 +03:00
committed by GitHub
parent 5de33a91d1
commit 5ecf8300ef
3 changed files with 23 additions and 16 deletions

View File

@@ -1,9 +1,11 @@
#![allow(dead_code)]
use crate::config::Config;
use crate::executor::RunType;
use crate::git::Git;
use crate::utils::require_option;
use crate::{config::Config, executor::Executor};
use anyhow::Result;
use directories::BaseDirs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub struct ExecutionContext<'a> {
run_type: RunType,
@@ -30,6 +32,22 @@ impl<'a> ExecutionContext<'a> {
}
}
pub fn execute_elevated(&self, command: &Path, interactive: bool) -> Result<Executor> {
let sudo = require_option(self.sudo.clone(), "Sudo is required for this operation".into())?;
let mut cmd = self.run_type.execute(&sudo);
if sudo.ends_with("sudo") {
cmd.arg("--preserve-env=DIFFPROG");
}
if interactive {
cmd.arg("-i");
}
cmd.arg(command);
Ok(cmd)
}
pub fn run_type(&self) -> RunType {
self.run_type
}