refactor: unify the behavior of the steps that require sudo (#467)

This commit is contained in:
SteveLauC
2023-06-13 22:15:57 +08:00
committed by GitHub
parent a7ddf4575a
commit 30858780cf
11 changed files with 265 additions and 309 deletions

View File

@@ -15,7 +15,7 @@ use crate::command::{CommandExt, Utf8Output};
use crate::execution_context::ExecutionContext;
use crate::executor::ExecutorOutput;
use crate::terminal::{print_separator, shell};
use crate::utils::{self, require, require_option, which, PathExt};
use crate::utils::{self, require, require_option, which, PathExt, REQUIRE_SUDO};
use crate::Step;
use crate::HOME_DIR;
use crate::{
@@ -112,7 +112,8 @@ pub fn run_rubygems(ctx: &ExecutionContext) -> Result<()> {
.execute(gem)
.args(["update", "--system"])
.status_checked()?;
} else if let Some(sudo) = &ctx.sudo() {
} else {
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
if !Path::new("/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb").exists() {
ctx.run_type()
.execute(sudo)
@@ -121,9 +122,8 @@ pub fn run_rubygems(ctx: &ExecutionContext) -> Result<()> {
.args(["update", "--system"])
.status_checked()?;
}
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
@@ -142,9 +142,8 @@ pub fn run_haxelib_update(ctx: &ExecutionContext) -> Result<()> {
let mut command = if directory_writable {
ctx.run_type().execute(&haxelib)
} else {
let mut c = ctx
.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?);
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
let mut c = ctx.run_type().execute(sudo);
c.arg(&haxelib);
c
};
@@ -310,9 +309,8 @@ pub fn run_vcpkg_update(ctx: &ExecutionContext) -> Result<()> {
let mut command = if is_root_install {
ctx.run_type().execute(&vcpkg)
} else {
let mut c = ctx
.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?);
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
let mut c = ctx.run_type().execute(sudo);
c.arg(&vcpkg);
c
};
@@ -510,9 +508,8 @@ pub fn run_tlmgr_update(ctx: &ExecutionContext) -> Result<()> {
let mut command = if directory_writable {
ctx.run_type().execute(&tlmgr)
} else {
let mut c = ctx
.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?);
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
let mut c = ctx.run_type().execute(sudo);
c.arg(&tlmgr);
c
};
@@ -592,8 +589,9 @@ pub fn run_composer_update(ctx: &ExecutionContext) -> Result<()> {
};
if has_update {
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
ctx.run_type()
.execute(ctx.sudo().as_ref().unwrap())
.execute(sudo)
.arg(&composer)
.arg("self-update")
.status_checked()?;
@@ -676,14 +674,15 @@ pub fn run_helix_grammars(ctx: &ExecutionContext) -> Result<()> {
print_separator("Helix");
let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;
ctx.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?)
.execute(sudo)
.args(["helix", "--grammar", "fetch"])
.status_checked()
.with_context(|| "Failed to download helix grammars!")?;
ctx.run_type()
.execute(ctx.sudo().as_ref().ok_or(TopgradeError::SudoRequired)?)
.execute(sudo)
.args(["helix", "--grammar", "build"])
.status_checked()
.with_context(|| "Failed to build helix grammars!")?;