Skip to content

Commit

Permalink
Execute-elevated (r-darwish#892)
Browse files Browse the repository at this point in the history
* Introduce the execute elevated method (fix r-darwish#885)

* fmt

* Fix nix with doas

* Bad import
  • Loading branch information
r-darwish authored Apr 9, 2022
1 parent 5de33a9 commit 5ecf830
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
22 changes: 20 additions & 2 deletions src/execution_context.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions src/steps/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,7 @@ pub fn run_config_update(ctx: &ExecutionContext) -> Result<()> {
}

print_separator("Configuration update");
ctx.run_type()
.execute(sudo)
.arg("--preserve-env=DIFFPROG")
.arg(pacdiff)
.check_run()?;
ctx.execute_elevated(&pacdiff, false)?.check_run()?;
}

Ok(())
Expand Down
11 changes: 2 additions & 9 deletions src/steps/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::execution_context::ExecutionContext;
#[cfg(target_os = "macos")]
use crate::executor::CommandExt;
use crate::executor::{Executor, ExecutorExitStatus, RunType};
use crate::terminal::{print_separator, print_warning};
use crate::terminal::print_separator;
use crate::utils::{require, require_option, PathExt};
use crate::Step;
use anyhow::Result;
Expand Down Expand Up @@ -232,14 +232,7 @@ pub fn run_nix(ctx: &ExecutionContext) -> Result<()> {
let run_type = ctx.run_type();

if multi_user {
if let Some(sudo) = ctx.sudo() {
run_type
.execute(&sudo)
.args(&["-i", "nix", "upgrade-nix"])
.check_run()?;
} else {
print_warning("Need sudo to upgrade Nix");
}
ctx.execute_elevated(&nix, true)?.arg("upgrade-nix").check_run()?;
} else {
run_type.execute(&nix).arg("upgrade-nix").check_run()?;
}
Expand Down

0 comments on commit 5ecf830

Please sign in to comment.