Automatically detect gsudo (#469)

This commit is contained in:
Roey Darwish Dror
2020-07-10 11:21:19 +03:00
committed by GitHub
parent 69b9c4b24c
commit 065565240e
5 changed files with 15 additions and 38 deletions

View File

@@ -385,6 +385,15 @@ impl Config {
check_deprecated!(config_file, yay_arguments, linux, yay_arguments); check_deprecated!(config_file, yay_arguments, linux, yay_arguments);
check_deprecated!(config_file, accept_all_windows_updates, windows, accept_all_updates); check_deprecated!(config_file, accept_all_windows_updates, windows, accept_all_updates);
if config_file
.windows
.as_ref()
.map(|w| w.use_gsudo_with_choco.is_some())
.unwrap_or(false)
{
println!("use_gsudo_with_choco is deprecated and will be removed in the future. Topgrade will not automatically detect and use gsudo");
}
let allowed_steps = Self::allowed_steps(&opt, &config_file); let allowed_steps = Self::allowed_steps(&opt, &config_file);
Ok(Self { Ok(Self {
@@ -526,16 +535,6 @@ impl Config {
.unwrap_or(false) .unwrap_or(false)
} }
/// Whether to use gsudo command with choco if available
#[allow(dead_code)]
pub fn use_gsudo_with_choco(&self) -> bool {
self.config_file
.windows
.as_ref()
.and_then(|w| w.use_gsudo_with_choco)
.unwrap_or(false)
}
/// Whether Brew cask should be greedy /// Whether Brew cask should be greedy
#[allow(dead_code)] #[allow(dead_code)]
pub fn brew_cask_greedy(&self) -> bool { pub fn brew_cask_greedy(&self) -> bool {

View File

@@ -14,7 +14,6 @@ pub struct ExecutionContext<'a> {
} }
impl<'a> ExecutionContext<'a> { impl<'a> ExecutionContext<'a> {
#[cfg(unix)]
pub fn new( pub fn new(
run_type: RunType, run_type: RunType,
sudo: &'a Option<PathBuf>, sudo: &'a Option<PathBuf>,
@@ -31,17 +30,6 @@ impl<'a> ExecutionContext<'a> {
} }
} }
#[cfg(not(unix))]
pub fn new(run_type: RunType, git: &'a Git, config: &'a Config, base_dirs: &'a BaseDirs) -> ExecutionContext<'a> {
ExecutionContext {
run_type,
sudo: &None,
config,
git,
base_dirs,
}
}
pub fn run_type(&self) -> RunType { pub fn run_type(&self) -> RunType {
self.run_type self.run_type
} }

View File

@@ -65,16 +65,11 @@ fn run() -> Result<()> {
let git = git::Git::new(); let git = git::Git::new();
let mut git_repos = git::Repositories::new(&git); let mut git_repos = git::Repositories::new(&git);
#[cfg(unix)]
let sudo = utils::sudo(); let sudo = utils::sudo();
let run_type = executor::RunType::new(config.dry_run()); let run_type = executor::RunType::new(config.dry_run());
#[cfg(unix)]
let ctx = execution_context::ExecutionContext::new(run_type, &sudo, &git, &config, &base_dirs); let ctx = execution_context::ExecutionContext::new(run_type, &sudo, &git, &config, &base_dirs);
#[cfg(not(unix))]
let ctx = execution_context::ExecutionContext::new(run_type, &git, &config, &base_dirs);
let mut runner = runner::Runner::new(&ctx); let mut runner = runner::Runner::new(&ctx);
#[cfg(feature = "self-update")] #[cfg(feature = "self-update")]

View File

@@ -3,26 +3,22 @@ use crate::execution_context::ExecutionContext;
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::powershell; use crate::powershell;
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{require, which}; use crate::utils::require;
use anyhow::Result; use anyhow::Result;
use std::process::Command; use std::process::Command;
pub fn run_chocolatey(ctx: &ExecutionContext) -> Result<()> { pub fn run_chocolatey(ctx: &ExecutionContext) -> Result<()> {
let choco = require("choco")?; let choco = require("choco")?;
let yes = ctx.config().yes(); let yes = ctx.config().yes();
let use_gsudo = ctx.config().use_gsudo_with_choco();
print_separator("Chocolatey"); print_separator("Chocolatey");
let mut cmd = choco; let mut cmd = &choco;
let mut args = vec!["upgrade", "all"]; let mut args = vec!["upgrade", "all"];
if use_gsudo { if let Some(sudo) = ctx.sudo() {
let gsudo = which("gsudo"); cmd = sudo;
if let Some(gsudo) = gsudo { args.insert(0, "choco");
cmd = gsudo;
args = vec!["choco", "upgrade", "all"];
}
} }
let mut command = ctx.run_type().execute(&cmd); let mut command = ctx.run_type().execute(&cmd);

View File

@@ -87,9 +87,8 @@ pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
} }
} }
#[cfg(unix)]
pub fn sudo() -> Option<PathBuf> { pub fn sudo() -> Option<PathBuf> {
which("sudo").or_else(|| which("pkexec")) which("sudo").or_else(|| which("gsudo")).or_else(|| which("pkexec"))
} }
pub fn editor() -> Vec<String> { pub fn editor() -> Vec<String> {