]> arthur.barton.de Git - ax-zsh.git/commitdiff
homebrew: Add a wrapper function for the brew(1) command
authorAlexander Barton <alex@barton.de>
Sat, 27 Aug 2022 15:41:11 +0000 (17:41 +0200)
committerAlexander Barton <alex@barton.de>
Sat, 27 Aug 2022 15:41:11 +0000 (17:41 +0200)
This wrapper function for the `brew`(1) command does the following:

- Detect the location of the "real" brew(1) command.
- Change user and group when the Homebrew installation is owned by a different
  user (to preserve sane file permissions).
- Set a relaxed umask(1) so that other users can use software installed by
  Homebrew.
- Call the "real" `brew`(1) command.

plugins/homebrew/README.md
plugins/homebrew/homebrew.zprofile
plugins/homebrew/homebrew.zshrc [new file with mode: 0644]

index 201b8dd6f5d1285b86bfe75c6b84be54383d2bc3..c64c961e5bdbd8a65e89ddd33d2974944f493212 100644 (file)
@@ -3,3 +3,13 @@
 Setup "Homebrew" ("The missing package manager for OS X", http://brew.sh):
 
 - Enable ZSH completions installed by Homebrew formulae.
+- Install a `brew` wrapper function.
+
+This wrapper function for the `brew`(1) command does the following:
+
+- Detect the location of the "real" brew(1) command.
+- Change user and group when the Homebrew installation is owned by a different
+  user (to preserve sane file permissions).
+- Set a relaxed umask(1) so that other users can use software installed by
+  Homebrew.
+- Call the "real" `brew`(1) command.
index f59d43edab86d9c1e77cafae23dd7b393cf8904a..84cb740d0347bfcc6f29dafe79eae2d680b2d814 100644 (file)
@@ -4,11 +4,6 @@
 # Make sure that "brew(1)" is installed
 (( $+commands[brew] )) || return 1
 
-if [[ -n "$AXZSH_PLUGIN_CHECK" ]]; then
-       # Make sure brew command is working
-       brew --version >/dev/null 2>&1 || return 1
-fi
-
 eval "$(brew shellenv)"
 
 for dir (
diff --git a/plugins/homebrew/homebrew.zshrc b/plugins/homebrew/homebrew.zshrc
new file mode 100644 (file)
index 0000000..ff27b11
--- /dev/null
@@ -0,0 +1,59 @@
+# AX-ZSH: Alex' Modular ZSH Configuration
+# homebrew.zshrc -- Setup Homebrew Package Manager
+
+# Make sure that "brew(1)" is installed
+(( $+commands[brew] )) || return 1
+
+if [[ -n "$AXZSH_PLUGIN_CHECK" ]]; then
+       # Make sure brew command is working
+       brew --version >/dev/null 2>&1 || return 1
+fi
+
+function brew() {
+       # This wrapper function for the brew(1) command does the following:
+       # - Detect the location of the "real" brew(1) command.
+       # - Change user and group when the Homebrew installation is owned by a
+       #   different user (to preserve sane file permissions).
+       # - Set a relaxed umask(1) so that other users can use software
+       #   installed by Homebrew.
+       # - Call the "real" brew(1) command.
+
+       if [ -x /home/linuxbrew/.linuxbrew/bin/brew ]; then
+               real_brew_cmd="/home/linuxbrew/.linuxbrew/bin/brew"
+       elif [ -x /opt/homebrew/bin/brew ]; then
+               real_brew_cmd="/opt/homebrew/bin/brew"
+       elif [ -x /usr/local/bin/brew ]; then
+               real_brew_cmd="/usr/local/bin/brew"
+       else
+               if [ "$1" != "shellenv" ] && [ "$1" != "--prefix" ]; then
+                       echo "Oops, real \"brew\" command not found!? [for \"$1\"]" >&2
+               fi
+               return 101
+       fi
+
+       brew_prefix=$("$real_brew_cmd" --prefix) || return 102
+
+       if [[ $(stat -c %u "$brew_prefix") -eq $UID ]]; then
+               # We are the owner of the Homebrew installation.
+               (
+                       [[ $# -eq 0 && -t 1 ]] \
+                               && echo "Running \"$real_brew_cmd\" ..."
+                       umask 0022 || return 103
+                       "$real_brew_cmd" "$@"
+               )
+       else
+               # We are a different user than the owner of the Homebew
+               # installation. So we need to change the user when running the
+               # real "brew" command!
+               priv_exec="umask 0022 || exit 103; \"$real_brew_cmd\" $*"
+               (
+                       cd /tmp
+                       user="$(stat -c %U "$brew_prefix")"
+                       group="$(stat -c %G "$brew_prefix")"
+                       [[ $# -eq 0 && -t 1 ]] \
+                               && echo "Running \"$real_brew_cmd\" as user \"$user:$group\" ..."
+                       sudo -u "$user" -g "$group" -- sh -c "$priv_exec"
+               )
+       fi
+       return $?
+}