(flatpak-sync) Improve logging output

Closes #2
This commit is contained in:
Marcel Kapfer 2023-07-20 22:03:59 +02:00
parent 00fc8ea318
commit e1af6b7c3d
Signed by: mmk2410
GPG Key ID: CADE6F0C09F21B09
1 changed files with 41 additions and 10 deletions

View File

@ -9,13 +9,35 @@ set -euo pipefail
APPS_LIST_PATH="${APPS_LIST_PATH:=$HOME/.config/flatpak-sync/apps.list}"
APP_NAME="flatpak-sync"
APP_COLOR="\033[0;35m"
INFO_COLOR="\033[0;32m"
ERR_COLOR="\033[1;31m"
NO_COLOR="\033[0m"
info() {
printf "$APP_COLOR[$APP_NAME] $INFO_COLOR$1$NO_COLOR\n"
}
error() {
printf "$APP_COLOR[$APP_NAME] $ERR_COLOR$1$NO_COLOR\n"
}
usage() {
info "Usage:"
info " -l Path to the file keeping the date of the last check."
info " -r Path to the directory containing all org-roam files."
info " -b Path to the hugo base directory."
info " -s Path of the Emacs lisp script for exporting the org-roam files."
}
function sync() {
readarray -t requested_apps < <(cat "$APPS_LIST_PATH")
readarray -t installed_apps < <(flatpak list --app --columns app | tail -n +1)
declare -a requested_apps_cleaned=()
echo "Checking for apps to install."
info "Checking for apps to install."
for app in "${requested_apps[@]}"; do
if [[ -z "$app" ]] || [[ "$app" =~ ^#.* ]]; then
continue
@ -23,16 +45,16 @@ function sync() {
requested_apps_cleaned+=("$app")
# shellcheck disable=SC2076
if [[ ! "${installed_apps[*]}" =~ "${app}" ]]; then
echo "$app not installed. Installing it."
info "$app not installed. Installing it."
flatpak install -y "$app"
fi
done
echo "Checking for apps to remove."
info "Checking for apps to remove."
for app in "${installed_apps[@]}"; do
# shellcheck disable=SC2076
if [[ ! "${requested_apps_cleaned[*]}" =~ "${app}" ]]; then
echo "$app no found in apps list. Removing it.";
info "$app no found in apps list. Removing it.";
flatpak uninstall -y "$app"
fi
done
@ -42,20 +64,25 @@ function add() {
local appId="${2:-}"
if [[ -z "$appId" ]]; then
echo "No application ID given."
error "No application ID given."
usage
exit 1
fi
if grep -q "# $appId" "$APPS_LIST_PATH"; then
echo "App already in apps list but commented out. Manual fix of the app list file necessary."
error "App $appId already in apps list but commented out."
error "Manual fix of the app list file necessary."
exit 2
fi
if grep -q "$appId" "$APPS_LIST_PATH"; then
echo "App already in apps list. Run with sync command to install."
error "App $appId already in apps list."
error "Run sync command to install it."
usage
exit 3
fi
info "Adding app $appId to apps list and installing it."
local appsListFile
appsListFile="$(realpath "$APPS_LIST_PATH")"
echo "$appId" >> "$appsListFile"
@ -65,20 +92,24 @@ function remove() {
local appId="${2:-}"
if [[ -z "$appId" ]]; then
echo "No application ID given."
error "No application ID given."
usage
exit 1
fi
if grep -q "# $appId" "$APPS_LIST_PATH"; then
echo "App already in apps list but commented out. No remove possible."
error "App $appId already in apps list but commented out."
error "No remove possible."
exit 2
fi
if ! grep -q "$appId" "$APPS_LIST_PATH"; then
echo "App not in apps list."
error "App $appId not in apps list."
usage
exit 2
fi
info "Removing app $appId from apps list and uninstalling it."
local appsListFile
appsListFile="$(realpath "$APPS_LIST_PATH")"
appsListTmpFile="$(mktemp)"