refactor: route sudo usage through Sudo::execute*

This commit is contained in:
Andre Toerien
2025-06-25 15:59:02 +02:00
committed by Gideon
parent 012a6bbde3
commit a95dd1e037
16 changed files with 303 additions and 321 deletions

View File

@@ -3,7 +3,6 @@ use color_eyre::eyre::Result;
use rust_i18n::t;
use std::env::var;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;
use std::sync::{LazyLock, Mutex};
@@ -11,8 +10,8 @@ use crate::executor::DryCommand;
use crate::powershell::Powershell;
#[cfg(target_os = "linux")]
use crate::steps::linux::Distribution;
use crate::sudo::{Sudo, SudoExecuteOpts};
use crate::utils::{get_require_sudo_string, require_option};
use crate::sudo::Sudo;
use crate::utils::require_option;
use crate::{config::Config, executor::Executor};
/// An enum telling whether Topgrade should perform dry runs or actually perform the steps.
@@ -87,20 +86,6 @@ impl<'a> ExecutionContext<'a> {
}
}
/// Create an instance of `Executor` that should run `program`,
/// using sudo to elevate privileges.
pub fn execute_elevated(&self, command: &Path, interactive: bool) -> Result<Executor> {
let sudo = require_option(self.sudo.as_ref(), get_require_sudo_string())?;
sudo.execute_opts(
self,
command,
SudoExecuteOpts {
interactive,
..Default::default()
},
)
}
pub fn run_type(&self) -> RunType {
self.run_type
}
@@ -109,6 +94,13 @@ impl<'a> ExecutionContext<'a> {
&self.sudo
}
pub fn require_sudo(&self) -> Result<&Sudo> {
require_option(
self.sudo.as_ref(),
t!("Require sudo or counterpart but not found, skip").to_string(),
)
}
pub fn config(&self) -> &Config {
self.config
}