Skip to content

Instantly share code, notes, and snippets.

@todmephis
Forked from streeter/brew-update-notifier.sh
Last active September 19, 2024 14:01
Show Gist options
  • Save todmephis/832796f8d5b148efa89df4ca7c63a1b4 to your computer and use it in GitHub Desktop.
Save todmephis/832796f8d5b148efa89df4ca7c63a1b4 to your computer and use it in GitHub Desktop.
Homebrew Package Update Notifications on macOS Mojave
#!/bin/bash
#
# Notify of Homebrew updates via Notification Center on macOS
# Forked from: https://gist.github.com/streeter/3254906
# https://github.com/julienXX/terminal-notifier
# https://github.com/vjeantet/alerter
# Author: Ivan Sanchez https://todmephis.cf/
# Twetter: https://twitter.com/todmephis
# Requires: terminal-notifier, alerter. Install with:
# brew install terminal-notifier
# https://github.com/vjeantet/alerter
# Tested on: macOS High Sierra Version 10.13.6
# NEW: tested on macOS Mojave Version 10.14.3
# Improvements:
# Added log file
# Added actions: Ignore notification, Show available packages, Install available packages (alerter needed).
# Add to crontab. Ex:
# $crontab -e
# 10 23 1 * * /Users/foo/.bin/brew-update-notifier.sh
USER=`whoami`
SCRIPT_PATH='/Users/'$USER'/.bin/' #set where the script lives in your mac.
LOGFILE='brew-update-notifier.log' #logfile name
BREW_EXEC=`which brew`
TERMINAL_NOTIFIER=`which terminal-notifier`
ALERTER=`which alerter`
NOTIF_ARGS="-sound default -timeout 10 -sender com.apple.systempreferences" #default args for Notification
$BREW_EXEC update 2>&1 > /dev/null
outdated=`$BREW_EXEC outdated | tr ' ' '\n'`
if [ -z "$outdated" ] ; then
if [ -e $TERMINAL_NOTIFIER ]; then
# No updates available, send to notification center and logfile
$TERMINAL_NOTIFIER \
-title "🍺 Homebrew" -subtitle "Everything up-to-date" \
-message "We couldn't find new updates" \
$NOTIF_ARGS
echo "===============================================" >> $SCRIPT_PATH$LOGFILE
date >> $SCRIPT_PATH$LOGFILE
echo "Everything up-to-date" >> $SCRIPT_PATH$LOGFILE
fi
else
# We've got an outdated formula or two
# Nofity via Notification Center and logfile
if [ -e $TERMINAL_NOTIFIER ]; then
lc=$((`echo "$outdated" | wc -l`))
outdated=`echo "$outdated" | tail -$lc`
message=`echo "$outdated" | head -5`
if [ "$outdated" != "$message" ]; then
message="Some of the outdated formulae are:
$message"
else
message="The following formulae are outdated:
$message"
fi
echo "===============================================" >> $SCRIPT_PATH$LOGFILE
date >> $SCRIPT_PATH$LOGFILE
echo "Updates available" >> $SCRIPT_PATH$LOGFILE
answer=$($ALERTER \
-title "🍺 Homebrew Update(s) Available" -message "$message" \
-closeLabel Ignore -actions Upgrade \
$NOTIF_ARGS)
#Take an action
case $answer in
#Notification ignored. Send to log.
"@TIMEOUT")
echo "===============================================" >> $SCRIPT_PATH$LOGFILE
date >> $SCRIPT_PATH$LOGFILE
echo "Notification timeout. No actions taken" >> $SCRIPT_PATH$LOGFILE
;;
#Clicked the notificaction. Send to logfile outdated packages
"@CONTENTCLICKED")
echo "===============================================" >> $SCRIPT_PATH$LOGFILE
date >> $SCRIPT_PATH$LOGFILE
echo "Outdated packages" >> $SCRIPT_PATH$LOGFILE
$BREW_EXEC outdated >> $SCRIPT_PATH$LOGFILE ; exit 0;
;;
#Clicked ignore option. Send to log file
"Ignore")
echo "===============================================" >> $SCRIPT_PATH$LOGFILE
date >> $SCRIPT_PATH$LOGFILE
echo "Notification has been ignored. No actions taken" >> $SCRIPT_PATH$LOGFILE
;;
#Clicked upgrade option. Upgrade outdated packages and send the output to logfile
"Upgrade")
echo "==============================================="
date
echo "Upgrade request"
$BREW_EXEC upgrade >> $SCRIPT_PATH$LOGFILE
#Send notification; upgrade complete.
$TERMINAL_NOTIFIER \
-title "🍺 Homebrew" -subtitle "Upgrade completed" \
-message "Check logfile for further information" \
$NOTIF_ARGS
;;
*)
exit 1
;;
esac
fi
fi
#by todmephis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment