Execute-elevated (#892)
* Introduce the execute elevated method (fix #885) * fmt * Fix nix with doas * Bad import
This commit is contained in:
committed by
GitHub
parent
5de33a91d1
commit
5ecf8300ef
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user