feat(winget): winget uses sudo when [windows] winget_use_sudo = true (#1061)

Co-authored-by: Gideon <87426140+GideonBear@users.noreply.github.com>
This commit is contained in:
Andreas Hoornstra
2025-07-15 09:12:55 +02:00
committed by GitHub
parent 23fff2a09f
commit 85c8bd2277
3 changed files with 35 additions and 1 deletions

View File

@@ -232,6 +232,16 @@
# manager such as Scoop or Cargo
# self_rename = true
# Use sudo to elevate privileges for the Windows Package Manager (winget)
# Only use this option if you want to run the Winget step in sudo-mode.
# Running winget in sudo-mode is generally not recommended, as not every
# package supports installing / upgrading in sudo-mode and it may cause issues
# with some packages or may even cause the Winget-step to fail.
# If any problems occur, please try running Topgrade without this option first
# before reporting an issue.
# (default: false)
# winget_use_sudo = true
[npm]
# Use sudo if the NPM directory isn't owned by the current user

View File

@@ -254,6 +254,7 @@ pub struct Windows {
wsl_update_pre_release: Option<bool>,
wsl_update_use_web_download: Option<bool>,
winget_silent_install: Option<bool>,
winget_use_sudo: Option<bool>,
}
#[derive(Deserialize, Default, Debug, Merge)]
@@ -1265,6 +1266,15 @@ impl Config {
.unwrap_or(false)
}
/// Should use sudo for Winget
pub fn winget_use_sudo(&self) -> bool {
self.config_file
.windows
.as_ref()
.and_then(|w| w.winget_use_sudo)
.unwrap_or(false)
}
/// Whether Brew cask should be greedy
pub fn brew_cask_greedy(&self) -> bool {
self.config_file

View File

@@ -47,12 +47,26 @@ pub fn run_winget(ctx: &ExecutionContext) -> Result<()> {
.args(["source", "update"])
.status_checked()?;
let mut command = if ctx.config().winget_use_sudo() {
match ctx.sudo() {
Some(sudo) => {
let mut command = ctx.run_type().execute(sudo);
command.arg(winget);
command
}
None => ctx.run_type().execute(winget),
}
} else {
ctx.run_type().execute(winget)
};
let mut args = vec!["upgrade", "--all"];
if ctx.config().winget_silent_install() {
args.push("--silent");
}
ctx.run_type().execute(&winget).args(args).status_checked()?;
command.args(args).status_checked()?;
Ok(())
}