Skip to content

Commit

Permalink
chore: Streamline option checking in arch-update.conf
Browse files Browse the repository at this point in the history
Streamline option checking in the `arch-update.conf` configuration file by optimizing the way each option are checked and the way the related variables are set.
  • Loading branch information
Antiz96 committed Sep 25, 2024
1 parent 7d43e0a commit 676b7f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 53 deletions.
7 changes: 1 addition & 6 deletions src/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ if [ -n "${diff_prog}" ]; then
fi
fi

# Definition of the tray icon style to use (default to "light" if it isn't set in the arch-update.conf configuration file)
if [ -z "${tray_icon_style}" ]; then
tray_icon_style="light"
fi

# Definition of the icon_up-to-date function: Change icon to "up to date"
# Definition of the icon_up-to-date function: Change tray icon to "up to date"
icon_up-to-date() {
# shellcheck disable=SC2154
echo "${name}-${tray_icon_style}" > "${statedir}/tray_icon"
Expand Down
67 changes: 20 additions & 47 deletions src/lib/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,41 @@ fi
config_file="${XDG_CONFIG_HOME:-${HOME}/.config}/${name}/${name}.conf"

# Check the "NoColor" option in arch-update.conf
if grep -Eq '^[[:space:]]*NoColor[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
no_color="y"
fi
# shellcheck disable=SC2034
no_color=$(grep -Eq '^[[:space:]]*NoColor[[:space:]]*$' "${config_file}" 2> /dev/null && echo "y")

# Check the "NoVersion" option in arch-update.conf
if grep -Eq '^[[:space:]]*NoVersion[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
no_version="y"
fi
# shellcheck disable=SC2034
no_version=$(grep -Eq '^[[:space:]]*NoVersion[[:space:]]*$' "${config_file}" 2> /dev/null && echo "y")

# Check the "AlwaysShowNews" option in arch-update.conf
if grep -Eq '^[[:space:]]*AlwaysShowNews[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
show_news="y"
fi
# shellcheck disable=SC2034
show_news=$(grep -Eq '^[[:space:]]*AlwaysShowNews[[:space:]]*$' "${config_file}" 2> /dev/null && echo "y")

# Check the "NewsNum" option in arch-update.conf
if grep -Eq '^[[:space:]]*NewsNum[[:space:]]*=[[:space:]]*[1-9][0-9]*[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
news_num=$(grep -E '^[[:space:]]*NewsNum[[:space:]]*=[[:space:]]*[1-9][0-9]*[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
else
# shellcheck disable=SC2034
news_num="5"
fi
# shellcheck disable=SC2034
news_num=$(grep -E '^[[:space:]]*NewsNum[[:space:]]*=[[:space:]]*[1-9][0-9]*[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]' || echo "5")

# Check the "AURHelper" option in arch-update.conf
if grep -Eq '^[[:space:]]*AURHelper[[:space:]]*=[[:space:]]*(paru|yay)[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
aur_helper=$(grep -E '^[[:space:]]*AURHelper[[:space:]]*=[[:space:]]*(paru|yay)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
fi
# shellcheck disable=SC2034
aur_helper=$(grep -E '^[[:space:]]*AURHelper[[:space:]]*=[[:space:]]*(paru|yay)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')

# Check the "PrivilegeElevationCommand" option in arch-update.conf
if grep -Eq '^[[:space:]]*PrivilegeElevationCommand[[:space:]]*=[[:space:]]*(sudo|doas|run0)[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
su_cmd=$(grep -E '^[[:space:]]*PrivilegeElevationCommand[[:space:]]*=[[:space:]]*(sudo|doas|run0)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
fi
# shellcheck disable=SC2034
su_cmd=$(grep -E '^[[:space:]]*PrivilegeElevationCommand[[:space:]]*=[[:space:]]*(sudo|doas|run0)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')

# Check the "KeepOldPackages" option in arch-update.conf
if grep -Eq '^[[:space:]]*KeepOldPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null; then
old_packages_num=$(grep -E '^[[:space:]]*KeepOldPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
else
# shellcheck disable=SC2034
old_packages_num="3"
fi
# shellcheck disable=SC2034
old_packages_num=$(grep -E '^[[:space:]]*KeepOldPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]' || echo "3")

# Check the "KeepUninstalledPackages" option in arch-update.conf
if grep -Eq '^[[:space:]]*KeepUninstalledPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null; then
uninstalled_packages_num=$(grep -E '^[[:space:]]*KeepUninstalledPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
else
# shellcheck disable=SC2034
uninstalled_packages_num="0"
fi
# shellcheck disable=SC2034
uninstalled_packages_num=$(grep -E '^[[:space:]]*KeepUninstalledPackages[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]' || echo "0")

# Check the "DiffProg" option in arch-update.conf
if grep -Eq '^[[:space:]]*DiffProg[[:space:]]*=[[:space:]]*[^[:space:]].*[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
diff_prog=$(grep -E '^[[:space:]]*DiffProg[[:space:]]*=[[:space:]]*[^[:space:]].*[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
fi
# shellcheck disable=SC2034
diff_prog=$(grep -E '^[[:space:]]*DiffProg[[:space:]]*=[[:space:]]*[^[:space:]].*[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')

# Check the "TrayIconStyle" option in arch-update.conf
if grep -Eq '^[[:space:]]*TrayIconStyle[[:space:]]*=[[:space:]]*(light|dark|blue)[[:space:]]*$' "${config_file}" 2> /dev/null; then
# shellcheck disable=SC2034
tray_icon_style=$(grep -E '^[[:space:]]*TrayIconStyle[[:space:]]*=[[:space:]]*(light|dark|blue)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]')
fi
# shellcheck disable=SC2034
tray_icon_style=$(grep -E '^[[:space:]]*TrayIconStyle[[:space:]]*=[[:space:]]*(light|dark|blue)[[:space:]]*$' "${config_file}" 2> /dev/null | awk -F '=' '{print $2}' | tr -d '[:space:]' || echo "light")

0 comments on commit 676b7f6

Please sign in to comment.