diff --git a/config.example.toml b/config.example.toml index 7ee2c947..479a0067 100644 --- a/config.example.toml +++ b/config.example.toml @@ -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 diff --git a/src/config.rs b/src/config.rs index 76d38eec..6bbe3e46 100644 --- a/src/config.rs +++ b/src/config.rs @@ -254,6 +254,7 @@ pub struct Windows { wsl_update_pre_release: Option, wsl_update_use_web_download: Option, winget_silent_install: Option, + winget_use_sudo: Option, } #[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 diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index d06ec971..c84beac9 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -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(()) }