Skip to content

Commit

Permalink
Restructuration of the functions' calls in the script to make it more…
Browse files Browse the repository at this point in the history
… readable and easier to evolve/work with (#68)

* Restructuration of the functions' calls in the script to make it more readable and easier to evolve/work with
This commit aims to restructure the functions calls so they are now completely independant from each other, in the sense that no function is making a direct call to another one anymore (except for the 'changing icon' ones).

This makes the script more readable and easier to evolve/work with, as now the functions calls are centralized in the last 'case' statement instead of having both the 'list_packages' and the 'update' function calling other functions depending on certains parameters (which made adding a new function unnecessarilly complex).

* Avoid the 'Press enter to quit' to show when rebooting to apply a pending kernel update

* Update documentation according to the functions' calls restructuration

* typo
  • Loading branch information
Antiz96 authored Dec 1, 2023
1 parent 9cbe9c3 commit 79dc68d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 81 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ Here is a little YouTube presentation/review of `arch-update` that [Cardiac](htt

The .desktop file is located in `/usr/share/applications/arch-update.desktop` (or `/usr/local/share/applications/arch-update.desktop` if you installed `arch-update` [from source](#from-source)).
Its icon will automatically change depending on the different states (checking for updates, updates available, installing updates, up to date).
It will launch the main `update` function when clicked (see the [Documentation](#documentation) chapter). It is easy to integrate with any DE/WM, dock, status/launch bar or app menu.
It will launch the relevant series of functions to perform a complete and proper update when clicked (see the [Documentation](#documentation) chapter). It is easy to integrate with any DE/WM, dock, status/launch bar or app menu.

### The systemd timer

There is a systemd service in `/usr/lib/systemd/user/arch-update.service` (or in `/etc/systemd/user/arch-update.service` if you installed `arch-update` [from source](#from-source)) that executes the `check` function when launched (see the [Documentation](#documentation) chapter).
To launch it automatically **at boot and then once every hour**, enable the associated systemd timer:
To launch it automatically **at boot and then once every hour**, enable the associated systemd timer (you can modify the auto-check cycle to your liking, see the [Tips and tricks - Modify the auto-check cycle](#modify-the-auto-check-cycle) chapter):

```bash
systemctl --user enable --now arch-update.timer
Expand All @@ -87,20 +87,20 @@ It is the first icon from the left.

![top_bar_up_to_date](https://github.com/Antiz96/arch-update/assets/53110319/794696a0-3452-4afd-8d64-a41d64225082)

When `arch-update` is checking for updates, the icon changes accordingly *(the `check` function is automatically triggered at boot and then once every hour if you enabled the [systemd timer](#the-systemd-timer) and can be manually triggered by running the `arch-update -c` command)*:
When `arch-update` is checking for updates, the icon changes accordingly (the `check` function is automatically triggered at boot and then once every hour if you enabled the [systemd timer](#the-systemd-timer) and can be manually triggered by running the `arch-update -c` command):

![top_bar_checking](https://github.com/Antiz96/arch-update/assets/53110319/27cc96c7-6871-4235-81d2-20bc4528fa18)

If there are available updates, the icon will show a bell sign and a desktop notification indicating the number of available updates will be sent *(requires [libnotify/notify-send](https://archlinux.org/packages/extra/x86_64/libnotify/ "libnotify package"))*:
If there are available updates, the icon will show a bell sign and a desktop notification indicating the number of available updates will be sent (requires [libnotify/notify-send](https://archlinux.org/packages/extra/x86_64/libnotify/ "libnotify package")):

![top_bar_update_available](https://github.com/Antiz96/arch-update/assets/53110319/e76be2e4-07b1-41db-8a5d-8cff89e904f6)
![notification](https://github.com/Antiz96/arch-update/assets/53110319/4d7fb15e-2d94-4740-9831-fe4dfd264c13)

When the icon is clicked, it launches the main `update` function which refreshes the list of packages available for updates, print it inside a terminal window and asks for the user's confirmation to proceed with the installation *(it can also be launched by running the `arch-update` command, requires [yay](https://aur.archlinux.org/packages/yay "yay") or [paru](https://aur.archlinux.org/packages/paru "paru") for AUR packages update support and [flatpak](https://archlinux.org/packages/extra/x86_64/flatpak/) for Flatpak packages update support)*:
When the icon is clicked, it launches the relevant series of functions to perform a complete and proper update starting by refreshing the list of packages available for updates, print it inside a terminal window and asks for the user's confirmation to proceed with the installation (it can also be launched by running the `arch-update` command, requires [yay](https://aur.archlinux.org/packages/yay "yay") or [paru](https://aur.archlinux.org/packages/paru "paru") for AUR packages update support and [flatpak](https://archlinux.org/packages/extra/x86_64/flatpak/) for Flatpak packages update support):

![main_update_function](https://github.com/Antiz96/arch-update/assets/53110319/43ff2d3a-a6d6-455b-9642-b11c42ed1985)

You can optionally configure `arch-update` to not show the version changes during the package listing *(see [Tips and tricks - Do not show package version changes](#do-not-show-package-version-changes)*:
You can optionally configure `arch-update` to not show the version changes during the package listing (see [Tips and tricks - Do not show package version changes](#do-not-show-package-version-changes):

![main_update_function_without_version_changes](https://github.com/Antiz96/arch-update/assets/53110319/76827a27-be4f-4937-b231-53be62d9115f)

Expand Down Expand Up @@ -135,10 +135,10 @@ Finally, `arch-update` will check if there's a pending kernel update requiring a
## Documentation

```text
Run arch-update to perform the main "update" function:
Run arch-update to launch the relevant series of functions to perform a complete and proper update:
Print the list of packages available for update, then ask for the user's confirmation to proceed with the installation.
Before performing the update, offer to print the latest Arch Linux news.
Post update, check for orphan/unused packages and pacnew/pacsave files and, if there are, offers to process them.
Post update, check for orphan/unused packages, pacnew/pacsave files and pending kernel update requiring a reboot to be applied and, if there are, offers to process them.
Options:
-c, --check Check for available updates, send a desktop notification containing the number of available updates (if libnotify is installed)
Expand Down Expand Up @@ -180,7 +180,7 @@ See <https://wiki.archlinux.org/title/Desktop_notifications>
If you enabled the systemd.timer, the `--check` option is automatically launched at boot and then once every hour.

If you want to change that cycle, you can edit the `/usr/lib/systemd/user/arch-update.timer` file (or `/etc/systemd/user/arch-update.timer` if you installed `arch-update` [from source](#from-source)) and modify the `OnUnitActiveSec` value.
The timer needs to be re-enabled to apply changes, you can do so by typing the following command:
The timer needs to be re-enabled to apply changes, you can do so by running the following command:

```bash
systemctl --user enable --now arch-update.timer
Expand Down
8 changes: 4 additions & 4 deletions doc/man/arch-update.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ An update notifier/applier for Arch Linux that assists you with important pre/po

.SH OPTIONS
.PP
.RB "If no option is passed, perform the main " "update " "function: Print the list of packages available for update, then ask for the user's confirmation to proceed with the installation (" "pacman -Syu" ")."
.RB "If no option is passed, launch the relevant series of functions to perform a complete and proper update starting by printing the list of packages available for update, then ask for the user's confirmation to proceed with the installation."
.br
.RB "It also supports AUR packages update (if " "yay " "or " "paru " "is installed) and Flatpak packages update (if " "flatpak " "is installed)."
.br
.RB "Before performing the update, it offers to print the latest Arch Linux news to the user. Arch news that have been published within the last 15 days are tagged as '[NEW]'."
.br
.RB "It also checks for orphan packages, unused Flatpak packages, pacnew/pacsave files and pending kernel update requiring a reboot to be applied and, if there are, offers to process them."
.br
.RB "The " "update " "function is launched when you click on the (.desktop) icon."
.RB "Those functions are launched when you click on the (.desktop) icon."
.PP

.TP
Expand Down Expand Up @@ -83,7 +83,7 @@ Error when calling the reboot command to apply a pending kernel update

.TP
.B The systemd timer
.RB "There is a systemd service in " "/usr/lib/systemd/user/arch-update.service " "(or in " "/etc/systemd/user/arch-update.service " "if you installed arch-update from source) that executes the " "\-\-check " "function when launched. To launch it automatically " "at boot and then once every hour " "enable the associated systemd timer:"
.RB "There is a systemd service in " "/usr/lib/systemd/user/arch-update.service " "(or in " "/etc/systemd/user/arch-update.service " "if you installed arch-update from source) that executes the " "\-\-check " "function when launched. To launch it automatically " "at boot and then once every hour " "enable the associated systemd timer (the auto-check cycle can be modified to your liking. See the TIPS AND TRICKS chapter below):"
.br
.B systemctl \-\-user enable \-\-now arch-update.timer

Expand Down Expand Up @@ -114,7 +114,7 @@ See https://wiki.archlinux.org/title/Desktop_notifications
.br
.RB "If you want to change that cycle, you can edit the " "/usr/lib/systemd/user/arch-update.timer " "(or "/etc/systemd/user/arch-update.timer " if you installed arch-update from source) file and modify the " "OnUnitActiveSec " "value"
.br
The timer needs to be re-enabled to apply changes, you can do so by launching the following command:
The timer needs to be re-enabled to apply changes, you can do so by running the following command:
.br
.B systemctl --user enable --now arch-update.timer
.br
Expand Down
134 changes: 66 additions & 68 deletions src/script/arch-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,72 @@ invalid_option() {
exit 1
}

# Definition of the icon_checking function: Change icon to "checking" (used in the "list_packages" and "check" functions)
# Definition of the icon_checking function: Change icon to "checking"
icon_checking() {
cp -f /usr/share/icons/arch-update/arch-update_checking.svg /usr/share/icons/arch-update/arch-update.svg || exit 3
}

# Definition of the icon_updates_available function: Change icon to "updates-available" (used in the "list_packages", "update" and "check" functions)
# Definition of the icon_updates_available function: Change icon to "updates-available"
icon_updates_available() {
cp -f /usr/share/icons/arch-update/arch-update_updates-available.svg /usr/share/icons/arch-update/arch-update.svg || exit 3
}

# Definition of the icon_installing function: Change icon to "installing" (used in the "list_packages" function)
# Definition of the icon_installing function: Change icon to "installing"
icon_installing() {
cp -f /usr/share/icons/arch-update/arch-update_installing.svg /usr/share/icons/arch-update/arch-update.svg || exit 3
}

# Definition of the icon_up_to_date function: Change icon to "up to date" (used in the "list_packages", "update" and "check" functions)
# Definition of the icon_up_to_date function: Change icon to "up to date"
icon_up_to_date() {
cp -f /usr/share/icons/arch-update/arch-update_up-to-date.svg /usr/share/icons/arch-update/arch-update.svg || exit 3
}

# Definition of the list_packages function: Print packages that are available for update and offer to apply them if there are (used in the "update" functions)
# Definition of the check function: Check for available updates, change the icon accordingly and send a desktop notification containing the number of available updates
check() {
icon_checking

if [ -n "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_available=$(checkupdates ; "${aur_helper}" -Qua ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
elif [ -n "${aur_helper}" ] && [ -z "${flatpak}" ]; then
update_available=$(checkupdates ; "${aur_helper}" -Qua)
elif [ -z "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_available=$(checkupdates ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
else
update_available=$(checkupdates)
fi

if [ -n "${notif}" ]; then
statedir="${XDG_STATE_HOME:-${HOME}/.local/state}/${name}"
mkdir -p "${statedir}"

echo "${update_available}" > "${statedir}/current_check"
sed -i '/^\s*$/d' "${statedir}/current_check"
fi

if [ -n "${update_available}" ]; then
icon_updates_available

if [ -n "${notif}" ]; then
if ! diff "${statedir}/current_check" "${statedir}/last_check" &> /dev/null; then
update_number=$(wc -l "${statedir}/current_check" | awk '{print $1}')

if [ "${update_number}" -eq 1 ]; then
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} update available"
else
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} updates available"
fi
fi
fi
else
icon_up_to_date
fi

if [ -f "${statedir}/current_check" ]; then
mv -f "${statedir}/current_check" "${statedir}/last_check"
fi
}

# Definition of the list_packages function: Print packages that are available for update and offer to apply them if there are
list_packages() {
icon_checking

Expand Down Expand Up @@ -110,17 +155,13 @@ list_packages() {
if [ -z "${packages}" ] && [ -z "${aur_packages}" ] && [ -z "${flatpak_packages}" ]; then
icon_up_to_date
echo -e "No update available\n"
orphan_packages
pacnew_files
kernel_reboot
exit 0
else
icon_updates_available
read -rp $'Proceed with update? [Y/n] ' answer

case "${answer}" in
[Yy]|"")
icon_installing
proceed_with_update="y"
;;
*)
echo -e >&2 "The update has been aborted\n" && read -n 1 -r -s -p $'Press \"enter\" to quit\n'
Expand All @@ -130,7 +171,7 @@ list_packages() {
fi
}

# Definition of the list_news function: Print the latest Arch news and offers to read them (used in the "update" function)
# Definition of the list_news function: Print the latest Arch news and offers to read them
list_news() {
redo="y"

Expand Down Expand Up @@ -172,8 +213,7 @@ list_news() {

# Definition of the update function: Update packages
update() {
list_packages
list_news
icon_installing

if [ -n "${packages}" ]; then
echo -e "\n--Updating Packages--"
Expand Down Expand Up @@ -204,14 +244,9 @@ update() {

icon_up_to_date
echo -e "\nThe update has been applied\n"

orphan_packages
pacnew_files
kernel_reboot

}

# Definition of the orphan_packages function: Print orphan packages and offer to remove them if there are (used in the "list_packages" and "update" functions)
# Definition of the orphan_packages function: Print orphan packages and offer to remove them if there are
orphan_packages() {
orphan_packages=$(pacman -Qtdq)

Expand Down Expand Up @@ -266,7 +301,7 @@ orphan_packages() {
fi
}

# Definition of the pacnew_files function: Print pacnew files and offer to process them if there are (used in the "list_packages" and "update" functions)
# Definition of the pacnew_files function: Print pacnew files and offer to process them if there are
pacnew_files() {
pacnew_files=$(pacdiff -o)

Expand Down Expand Up @@ -309,6 +344,8 @@ kernel_reboot() {
if ! reboot; then
echo -e >&2 "\nAn error has occurred\nThe reboot has been aborted\n" && read -n 1 -r -s -p $'Press \"enter\" to quit\n'
exit 6
else
exit 0
fi
;;
*)
Expand All @@ -318,59 +355,20 @@ kernel_reboot() {
else
echo -e "No pending kernel update found\n"
fi

read -n 1 -r -s -p $'Press \"enter\" to quit\n'
}

# Definition of the check function: Check for available updates, change the icon accordingly and send a desktop notification containing the number of available updates
check() {
icon_checking

if [ -n "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_available=$(checkupdates ; "${aur_helper}" -Qua ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
elif [ -n "${aur_helper}" ] && [ -z "${flatpak}" ]; then
update_available=$(checkupdates ; "${aur_helper}" -Qua)
elif [ -z "${aur_helper}" ] && [ -n "${flatpak}" ]; then
update_available=$(checkupdates ; flatpak update | awk '{print $2}' | grep -v '^$' | sed '1d;$d')
else
update_available=$(checkupdates)
fi

if [ -n "${notif}" ]; then
statedir="${XDG_STATE_HOME:-${HOME}/.local/state}/${name}"
mkdir -p "${statedir}"

echo "${update_available}" > "${statedir}/current_check"
sed -i '/^\s*$/d' "${statedir}/current_check"
fi

if [ -n "${update_available}" ]; then
icon_updates_available

if [ -n "${notif}" ]; then
if ! diff "${statedir}/current_check" "${statedir}/last_check" &> /dev/null; then
update_number=$(wc -l "${statedir}/current_check" | awk '{print $1}')

if [ "${update_number}" -eq 1 ]; then
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} update available"
else
notify-send -i /usr/share/icons/arch-update/arch-update_updates-available.svg "Arch Update" "${update_number} updates available"
fi
fi
fi
else
icon_up_to_date
fi

if [ -f "${statedir}/current_check" ]; then
mv -f "${statedir}/current_check" "${statedir}/last_check"
fi
}

# Execute the different functions depending on the option
case "${option}" in
"")
update
list_packages
if [ -n "${proceed_with_update}" ]; then
list_news
update
fi
orphan_packages
pacnew_files
kernel_reboot
read -n 1 -r -s -p $'Press \"enter\" to quit\n'
;;
-c|--check)
check
Expand Down

0 comments on commit 79dc68d

Please sign in to comment.