Skip to content

Instantly share code, notes, and snippets.

@qexat
Last active September 18, 2024 10:39
Show Gist options
  • Save qexat/46ab3a1c3415de6c9389a2750004434a to your computer and use it in GitHub Desktop.
Save qexat/46ab3a1c3415de6c9389a2750004434a to your computer and use it in GitHub Desktop.
Arch Linux: Check updates and perform upgrades if available (requires yay)
#!/usr/bin/env fish
##################################################################
# Check if there are updates available. #
# -------------------------------------------------------------- #
# First, it runs `checkupdates` (with `-d` to prefetch). If it #
# returns 2, this means it has not found any updates. We then #
# query the AUR database to verify if there are updates on their #
# side. #
# #
# = Exit codes ================================================= #
# 0: updates have been found ; ready to upgrade. #
# 1: an unknown error has happened ; should break. #
# 2: no update available. #
##################################################################
function _check_updates
sudo checkupdates -d
switch "$status"
case 1
return 1
case 2
# Queries (-Q) the AUR (-a) for updates (-u).
yay -Qau
# yay returns 1 if no updates are available.
if test "$status" -eq 1
return 2
end
end
# Happy case (updates have been found).
return 0
end
##################################################################
# Perform updates. #
# -------------------------------------------------------------- #
# First, runs `yay` with `--quiet` to avoid too much output. #
# We also set `--sudoloop` in case there are packages that take #
# a while to build, to avoid sudo prompting. We also use #
# `--noconfirm` to avoid interruptions. #
# #
# If `yay` failed for some reason (e.g. pacman updated and yay #
# did not follow yet), we fall back to the classic `pacman`, but #
# this also means we won't get AUR upgrades. #
# #
# The exit code is equal to the one of `yay` or `pacman`. #
##################################################################
function _make_updates
yay --quiet --sudoloop --noconfirm
if test "$status" -ne 0
error "yay" "failed for an unknown reason"
fishmsg Info 4 "falling back to pacman: no AUR upgrade will be performed"
sudo pacman -Syyu --quiet --noconfirm
end
# We pass on the exit code.
return "$status"
end
##################################################################
# Main function. #
# -------------------------------------------------------------- #
# 1. Check updates #
# 2. Perform the upgrades if there are ones available. #
# #
# = Exit codes ================================================= #
# 0: if there is either no update, or the upgrades went without #
# any issue. #
# 1: there was a problem while checking/upgrading. #
##################################################################
function update
printf "\x1b[1m%s\x1b[22m\n" "◉ Checking updates..."
_check_updates
switch "$status"
case 1
error "checkupdates" "failed for an unknown reason"
return 1
case 2
fishmsg Info 4 "checkupdates" "no updates available"
fishmsg Info 4 "there is nothing to do"
return 0
end
# Visual separator between the two operations
printf "%s\n" (string repeat -n "$COLUMNS" "-")
printf "\x1b[1m%s\x1b[22m\n" "◉ Updating..."
_make_updates
if test "$status" -ne 0
error "make_updates" "failed for an unknown reason"
return 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment