Skip to content

Commit

Permalink
Merge pull request #110 from swissbuechi/add-sudo-gui
Browse files Browse the repository at this point in the history
  • Loading branch information
DomT4 authored Nov 11, 2023
2 parents ed3c3c3 + 59edcb0 commit c70c2bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ brew autoupdate version:
instead of waiting for one interval (24
hours by default) to pass first. Must be
passed with start.
--sudo If a Cask requires sudo, autoupdate will
open a GUI to ask for the password.
Requires https://formulae.brew.sh/formula/pinentry-mac
to be installed.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
Expand Down Expand Up @@ -109,11 +113,6 @@ that doesn't require or use any external dependencies, using only an Applescript
applet.
[Related Issue](https://github.com/Homebrew/homebrew-autoupdate/issues/25)

* Decide what to do about Cask upgrades which require `sudo` to succeed
and currently just hang when that situation is encountered,
unless using `SUDO_ASKPASS`.
[Related Issue](https://github.com/Homebrew/homebrew-autoupdate/issues/40)

## History

This tap was created by [DomT4](https://github.com/DomT4) in April 2015 to
Expand Down
3 changes: 3 additions & 0 deletions cmd/autoupdate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def autoupdate_args
description: "Starts the autoupdate command immediately and on system boot, " \
"instead of waiting for one interval (24 hours by default) to pass first. " \
"Must be passed with `start`."
switch "--sudo",
description: "If a Cask requires sudo, autoupdate will open a GUI to ask for the password." \
"Requires https://formulae.brew.sh/formula/pinentry-mac to be installed."

named_args SUBCOMMANDS
end
Expand Down
34 changes: 27 additions & 7 deletions lib/autoupdate/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def start(interval:, args:)
# comfortable enough with it I can tolerate it. Please consider the
# risks of leaving your admin password laying around the system in
# plaintext before using this, if you have no other use case for SUDO_ASKPASS.
if ENV["SUDO_ASKPASS"].nil?
if ENV["SUDO_ASKPASS"].nil? && !args.sudo?
opoo <<~EOS
Please note if you use Casks that require `sudo` to upgrade there
are known issues with that use case and this command unless using
`SUDO_ASKPASS`.
https://github.com/Homebrew/homebrew-autoupdate/issues/40
Please note if you use Casks that require `sudo` to upgrade,
you need to use `--sudo` or define a custom `SUDO_ASKPASS`
environment variable.
EOS
end
Expand Down Expand Up @@ -93,7 +91,24 @@ def start(interval:, args:)
set_env << "\nexport HOMEBREW_DEVELOPER=#{env_dev}" if env_dev
set_env << "\nexport HOMEBREW_NO_ANALYTICS=#{env_stats}" if env_stats
set_env << "\nexport HOMEBREW_CASK_OPTS=#{env_cask}" if env_cask
set_env << "\nexport SUDO_ASKPASS=#{env_sudo}" if env_sudo

if args.sudo?
unless Formula["pinentry-mac"].any_version_installed?
odie <<~EOS
`--sudo` requires https://formulae.brew.sh/formula/pinentry-mac to be installed.
Please run `brew install pinentry-mac` and try again.
EOS
end
set_env << "\nexport SUDO_ASKPASS='#{Autoupdate::Core.location/"brew_autoupdate_sudo_gui"}'"
sudo_gui_script_contents = <<~EOS
#!/bin/sh
export PATH='#{env_path}'
PW="$(printf "%s\n" "SETOK OK" "SETCANCEL Cancel" "SETDESC homebrew-autoupdate needs your admin password to complete the upgrade" "SETPROMPT Enter Password:" "SETTITLE homebrew-autoupdate Password Request" "GETPIN" | pinentry-mac --no-global-grab | awk '/^D / {print substr($0, index($0, $2))}')"
echo "$PW"
EOS
elsif env_sudo
set_env << "\nexport SUDO_ASKPASS=#{env_sudo}"
end

script_contents = <<~EOS
#!/bin/sh
Expand Down Expand Up @@ -135,6 +150,11 @@ def start(interval:, args:)
FileUtils.chmod 0555, Autoupdate::Core.location/"brew_autoupdate"
end

unless File.exist?(Autoupdate::Core.location/"brew_autoupdate_sudo_gui")
File.open(Autoupdate::Core.location/"brew_autoupdate_sudo_gui", "w") { |sc| sc << sudo_gui_script_contents }
FileUtils.chmod 0555, Autoupdate::Core.location/"brew_autoupdate_sudo_gui"
end

interval ||= "86400"

# This restores the "Run At Load" key removed in a7de771abcf6 when requested.
Expand Down

0 comments on commit c70c2bc

Please sign in to comment.