Skip to content

Commit 7defd39

Browse files
CopilotVen0m0
andauthored
Refactor duplicated code into shared libraries (#73)
* Initial plan * Create shared library structure and refactor duplicated code - Create lib/core.sh: common shell settings, colors, logging, helpers - Create lib/arch.sh: Arch Linux package manager detection, build env - Create lib/debian.sh: Debian/DietPi helpers, APT functions - Create lib/browser.sh: Mozilla/Chromium profile detection, SQLite - Refactor Cachyos/Updates.sh to use shared libraries - Refactor Cachyos/debloat.sh to use shared libraries - Refactor Cachyos/clean.sh to use shared libraries - Refactor RaspberryPi/PiClean.sh to use shared libraries - Refactor RaspberryPi/update.sh to use shared libraries - Refactor RaspberryPi/Scripts/Fix.sh to use shared libraries Co-authored-by: Ven0m0 <[email protected]> * Address code review: add fdf as first preference in find functions Co-authored-by: Ven0m0 <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Ven0m0 <[email protected]>
1 parent be673df commit 7defd39

10 files changed

Lines changed: 824 additions & 376 deletions

File tree

Cachyos/Updates.sh

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,22 @@
22
# Optimized: 2025-11-30 - Add support for soar and zoi; review phase triggers; clean env vars
33
# Standalone system update script for Arch-based systems.
44

5-
set -euo pipefail
6-
IFS=$'\n\t'
7-
shopt -s nullglob globstar
8-
9-
# Color definitions
10-
GRN=$'\e[32m'
11-
BLU=$'\e[34m'
12-
DEF=$'\e[0m'
13-
14-
export HOME="/home/${SUDO_USER:-$USER}"
15-
16-
has(){ command -v -- "$1" &>/dev/null; }
17-
18-
log(){ printf '%b\n' "$*"; }
19-
20-
_PKG_MGR_CACHED=""
21-
_AUR_OPTS_CACHED=()
22-
23-
get_pkg_manager(){
24-
if [[ -z $_PKG_MGR_CACHED ]]; then
25-
local pkgmgr
26-
if has paru; then
27-
pkgmgr=paru
28-
_AUR_OPTS_CACHED=(--batchinstall --combinedupgrade --nokeepsrc)
29-
elif has yay; then
30-
pkgmgr=yay
31-
_AUR_OPTS_CACHED=(--answerclean y --answerdiff n --answeredit n --answerupgrade y)
32-
else
33-
pkgmgr=pacman
34-
_AUR_OPTS_CACHED=()
35-
fi
36-
_PKG_MGR_CACHED="$pkgmgr"
37-
fi
38-
printf '%s\n' "$_PKG_MGR_CACHED"
39-
}
40-
41-
get_aur_opts(){
42-
[[ -z $_PKG_MGR_CACHED ]] && get_pkg_manager >/dev/null
43-
printf '%s\n' "${_AUR_OPTS_CACHED[@]}"
44-
}
5+
# Source shared libraries
6+
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" && pwd)"
7+
# shellcheck source=lib/core.sh
8+
source "$SCRIPT_DIR/../lib/core.sh"
9+
# shellcheck source=lib/arch.sh
10+
source "$SCRIPT_DIR/../lib/arch.sh"
4511

4612
main(){
47-
cleanup(){ sudo rm -f /var/lib/pacman/db.lck &>/dev/null || :; }
48-
trap cleanup EXIT INT TERM
13+
trap cleanup_pacman_lock EXIT INT TERM
4914
#============ Update Functions ============
5015
update_system(){
5116
local pkgmgr aur_opts
5217
log "🔄${BLU} System Packages${DEF}"
5318
pkgmgr=$(get_pkg_manager)
5419
mapfile -t aur_opts < <(get_aur_opts)
55-
cleanup
20+
cleanup_pacman_lock
5621
sudo "$pkgmgr" -Sy --needed archlinux-keyring --noconfirm || :
5722
[[ -f /var/lib/pacman/sync/core.files ]] || sudo pacman -Fy --noconfirm || :
5823
if [[ $pkgmgr == pacman ]]; then

Cachyos/clean.sh

Lines changed: 10 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,20 @@
11
#!/usr/bin/env bash
2-
set -euo pipefail
3-
shopt -s nullglob globstar
42
# Enhanced system cleaning with privacy configuration
53
# Refactored version with improved structure and maintainability
6-
IFS=$'\n\t'
7-
export LC_ALL=C LANG=C HOME="/home/${SUDO_USER:-$USER}"
8-
#============ Colors ============
9-
readonly LBLU=$'\e[38;5;117m'
10-
readonly PNK=$'\e[38;5;218m'
11-
readonly BWHT=$'\e[97m'
12-
readonly BLU=$'\e[34m'
13-
readonly GRN=$'\e[32m'
14-
readonly YLW=$'\e[33m'
15-
readonly MGN=$'\e[35m'
16-
readonly DEF=$'\e[0m'
4+
5+
# Source shared libraries
6+
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" && pwd)"
7+
# shellcheck source=lib/core.sh
8+
source "$SCRIPT_DIR/../lib/core.sh"
9+
# shellcheck source=lib/arch.sh
10+
source "$SCRIPT_DIR/../lib/arch.sh"
11+
# shellcheck source=lib/browser.sh
12+
source "$SCRIPT_DIR/../lib/browser.sh"
13+
1714
#============ Configuration ============
1815
declare -r MAX_PARALLEL_JOBS="$(nproc 2>/dev/null || echo 4)"
1916
declare -r SQLITE_TIMEOUT=30
2017

21-
#============ Helper Functions ============
22-
has(){ command -v -- "$1" &>/dev/null; }
23-
get_pkg_manager(){
24-
if has paru; then
25-
echo 'paru'
26-
elif has yay; then
27-
echo 'yay'
28-
else
29-
echo 'pacman'
30-
fi
31-
}
32-
33-
capture_disk_usage(){ df -h --output=used,pcent / 2>/dev/null | awk 'NR==2{print $1, $2}'; }
34-
35-
# Enhanced SQLite vacuum with reporting
36-
vacuum_sqlite(){
37-
local db=$1 s_old s_new saved
38-
[[ -f $db ]] || return 0
39-
[[ -f ${db}-wal || -f ${db}-journal ]] && return 0
40-
# Use fixed-string grep (-F) for faster matching
41-
head -c 16 "$db" 2>/dev/null | grep -qF -- 'SQLite format 3' || return 0
42-
s_old=$(stat -c%s "$db" 2>/dev/null) || return 0
43-
# VACUUM already rebuilds indices, REINDEX is redundant
44-
sqlite3 "$db" 'PRAGMA journal_mode=delete; VACUUM; PRAGMA optimize;' &>/dev/null || return 0
45-
s_new=$(stat -c%s "$db" 2>/dev/null) || s_new=$s_old
46-
saved=$((s_old - s_new))
47-
((saved > 0)) && echo "$saved"
48-
}
49-
50-
# Process SQLite databases with parallel processing
51-
clean_sqlite_dbs(){
52-
local total=0 saved db_list=() count=0
53-
while IFS= read -r -d '' db; do
54-
[[ -f $db ]] && db_list+=("$db")
55-
done < <(find . -maxdepth 2 -type f -name '*.sqlite*' -print0 2>/dev/null)
56-
[[ ${#db_list[@]} -eq 0 ]] && return 0
57-
if has parallel; then
58-
while IFS= read -r line; do
59-
[[ $line =~ ^[0-9]+$ ]] && {
60-
total=$((total + line))
61-
((count++))
62-
}
63-
done < <(printf '%s\n' "${db_list[@]}" | parallel -j"$MAX_PARALLEL_JOBS" vacuum_sqlite)
64-
elif has rust-parallel; then
65-
while IFS= read -r line; do
66-
[[ $line =~ ^[0-9]+$ ]] && {
67-
total=$((total + line))
68-
((count++))
69-
}
70-
done < <(printf '%s\n' "${db_list[@]}" | rust-parallel vacuum_sqlite)
71-
else
72-
for db in "${db_list[@]}"; do
73-
saved=$(vacuum_sqlite "$db" 2>/dev/null)
74-
[[ $saved =~ ^[0-9]+$ ]] && {
75-
total=$((total + saved))
76-
((count++))
77-
}
78-
done
79-
fi
80-
((total > 0)) && printf ' %s %s (%d files)\n' "${GRN}Vacuumed:" "$((total / 1024)) KB${DEF}" "$count"
81-
}
82-
83-
ensure_not_running(){
84-
local timeout=6 pattern
85-
pattern=$(printf '%s|' "$@")
86-
pattern=${pattern%|}
87-
pgrep -x -u "$USER" -f "$pattern" &>/dev/null || return 0
88-
for p in "$@"; do
89-
pgrep -x -u "$USER" "$p" &>/dev/null && printf ' %s\n' "${YLW}Waiting for ${p}...${DEF}"
90-
done
91-
local wait_time=$timeout
92-
while ((wait_time-- > 0)); do
93-
pgrep -x -u "$USER" -f "$pattern" &>/dev/null || return 0
94-
sleep 1
95-
done
96-
pkill -KILL -x -u "$USER" -f "$pattern" &>/dev/null || :
97-
}
98-
99-
# Mozilla profile discovery with IsRelative support
100-
mozilla_profiles(){
101-
local base=$1 p is_rel path_val
102-
[[ -d $base ]] || return 0
103-
if [[ -f $base/installs.ini ]]; then
104-
while IFS='=' read -r key val; do
105-
[[ $key == Default ]] && {
106-
path_val=$val
107-
[[ -d $base/$path_val ]] && echo "$base/$path_val"
108-
}
109-
done < <(grep -E '^Default=' "$base/installs.ini" 2>/dev/null)
110-
fi
111-
if [[ -f $base/profiles.ini ]]; then
112-
is_rel=1 path_val=''
113-
while IFS='=' read -r key val; do
114-
case $key in
115-
IsRelative) is_rel=$val ;;
116-
Path)
117-
path_val="$val"
118-
if [[ $is_rel -eq 0 ]]; then
119-
[[ -d $path_val ]] && echo "$path_val"
120-
else
121-
[[ -d $base/$path_val ]] && echo "$base/$path_val"
122-
fi
123-
path_val='' is_rel=1
124-
;;
125-
esac
126-
done < <(grep -E '^(IsRelative|Path)=' "$base/profiles.ini" 2>/dev/null)
127-
fi
128-
}
129-
130-
chrome_profiles(){
131-
local root="$1"
132-
for d in "$root"/Default "$root"/"Profile "*; do [[ -d $d ]] && echo "$d"; done
133-
}
134-
13518
configure_firefox_privacy(){
13619
local prefs_changed=0
13720
local -a firefox_prefs=(

Cachyos/debloat.sh

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@
22
# Unified Debloat Script for Arch-based and Debian-based systems
33
# Refactored version with improved structure and maintainability
44
# Removes unnecessary packages and disables telemetry services
5-
set -euo pipefail; shopt -s nullglob globstar
6-
IFS=$'\n\t'; export LC_ALL=C LANG=C HOME="/home/${SUDO_USER:-$USER}"
5+
6+
# Source shared libraries
7+
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" && pwd)"
8+
# shellcheck source=lib/core.sh
9+
source "$SCRIPT_DIR/../lib/core.sh"
10+
711
sudo -v
8-
# --- Colors ---
9-
readonly RED=$'\e[31m'
10-
readonly GRN=$'\e[32m'
11-
readonly YLW=$'\e[33m'
12-
readonly DEF=$'\e[0m'
13-
# --- Helper Functions ---
14-
has(){ command -v -- "$1" &>/dev/null; }
15-
msg(){
16-
printf '%b%s%b\n' "$GRN" "$*" "$DEF"
17-
}
18-
warn(){
19-
printf '%b%s%b\n' "$YLW" "$*" "$DEF"
20-
}
2112

2213
# --- Platform Detection ---
2314
detect_platform(){

RaspberryPi/PiClean.sh

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,13 @@
11
#!/usr/bin/env bash
22
# Optimized: 2025-11-21 - Applied bash optimization techniques
33
#──────────── Setup ────────────────────
4-
# Setup environment
5-
set -euo pipefail
6-
shopt -s nullglob globstar execfail
7-
IFS=$'\n\t'
8-
export LC_ALL=C LANG=C DEBIAN_FRONTEND=noninteractive
94

10-
# Initialize working directory
11-
WORKDIR="$(cd "${BASH_SOURCE[0]%/*}" && pwd)"
12-
cd "$WORKDIR" || {
13-
echo "Failed to change to working directory: $WORKDIR" >&2
14-
exit 1
15-
}
16-
# Check if a command exists
17-
has(){ command -v -- "$1" &>/dev/null; }
18-
# Get the name of a command from PATH
19-
hasname(){
20-
local x
21-
if ! x=$(type -P -- "$1"); then
22-
return 1
23-
fi
24-
printf '%s\n' "${x##*/}"
25-
}
26-
27-
# Load DietPi globals if available
28-
load_dietpi_globals(){ [[ -f /boot/dietpi/func/dietpi-globals ]] && . "/boot/dietpi/func/dietpi-globals" &>/dev/null || :; }
29-
30-
# Run DietPi cleanup commands if available
31-
run_dietpi_cleanup(){
32-
if [[ -f /boot/dietpi/func/dietpi-logclear ]]; then
33-
if ! sudo dietpi-update 1 && ! sudo /boot/dietpi/dietpi-update 1; then
34-
echo "Warning: dietpi-update failed (both standard and fallback commands)." >&2
35-
fi
36-
sudo /boot/dietpi/func/dietpi-logclear 2 2>/dev/null || G_SUDO dietpi-logclear 2 2>/dev/null || :
37-
sudo /boot/dietpi/func/dietpi-cleaner 2 2>/dev/null || G_SUDO dietpi-cleaner 2 2>/dev/null || :
38-
fi
39-
}
40-
41-
# Clean APT package manager cache
42-
clean_apt_cache(){
43-
sudo apt-get clean -y
44-
sudo apt-get autoclean -y
45-
sudo apt-get autoremove --purge -y
46-
}
47-
# Clean system cache directories
48-
clean_cache_dirs(){
49-
sudo rm -rf /tmp/* 2>/dev/null || :
50-
sudo rm -rf /var/tmp/* 2>/dev/null || :
51-
sudo rm -rf /var/cache/apt/archives/* 2>/dev/null || :
52-
rm -rf ~/.cache/* 2>/dev/null || :
53-
sudo rm -rf /root/.cache/* 2>/dev/null || :
54-
rm -rf ~/.thumbnails/* 2>/dev/null || :
55-
rm -rf ~/.cache/thumbnails/* 2>/dev/null || :
56-
}
57-
# Empty trash directories
58-
clean_trash(){
59-
rm -rf ~/.local/share/Trash/* 2>/dev/null || :
60-
sudo rm -rf /root/.local/share/Trash/* 2>/dev/null || :
61-
rm -rf ~/snap/*/*/.local/share/Trash/* 2>/dev/null || :
62-
rm -rf ~/.var/app/*/data/Trash/* 2>/dev/null || :
63-
}
64-
# Clean crash dumps and core dumps
65-
clean_crash_dumps(){
66-
if command -v coredumpctl &>/dev/null; then
67-
sudo coredumpctl --quiet --no-legend clean 2>/dev/null || :
68-
fi
69-
sudo rm -rf /var/crash/* 2>/dev/null || :
70-
sudo rm -rf /var/lib/systemd/coredump/* 2>/dev/null || :
71-
}
72-
# Clean shell and Python history files
73-
clean_history_files(){
74-
rm -f ~/.python_history 2>/dev/null || :
75-
sudo rm -f /root/.python_history 2>/dev/null || :
76-
rm -f ~/.bash_history 2>/dev/null || :
77-
sudo rm -f /root/.bash_history 2>/dev/null || :
78-
history -c 2>/dev/null || :
79-
}
80-
# Clean systemd journal logs
81-
clean_journal_logs(){
82-
sudo journalctl --rotate --vacuum-size=1 --flush --sync -q 2>/dev/null || :
83-
sudo rm -rf --preserve-root -- /run/log/journal/* /var/log/journal/* 2>/dev/null || :
84-
sudo systemd-tmpfiles --clean 2>/dev/null || :
85-
}
86-
#──────────── Sudo ────────────────────
5+
# Source shared libraries
6+
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" && pwd)"
7+
# shellcheck source=lib/core.sh
8+
source "$SCRIPT_DIR/../lib/core.sh"
9+
# shellcheck source=lib/debian.sh
10+
source "$SCRIPT_DIR/../lib/debian.sh"
8711
load_dietpi_globals
8812
sync
8913
#─────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)