diff options
| author | kj_sh604 | 2026-01-17 20:36:54 -0500 |
|---|---|---|
| committer | kj_sh604 | 2026-01-17 20:36:54 -0500 |
| commit | 01257ea68f3828d8f5c798c8f7bc3e07c14aca46 (patch) | |
| tree | 3d6017d14fdaa352f59cc0b25596fb74b9d7d514 | |
| parent | 356d37efc6e66a9c87a2690cfd24fab54701484d (diff) | |
feat: add a paru/pacman clean-up script
| -rwxr-xr-x | .local/bin/parucache-clean-up | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/.local/bin/parucache-clean-up b/.local/bin/parucache-clean-up new file mode 100755 index 0000000..ec96aa2 --- /dev/null +++ b/.local/bin/parucache-clean-up | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | script_name=$(basename "$0") | ||
| 4 | |||
| 5 | show_help() { | ||
| 6 | cat <<EOF | ||
| 7 | usage: $script_name [OPTIONS] | ||
| 8 | |||
| 9 | clean up pacman and paru cache. | ||
| 10 | |||
| 11 | OPTIONS: | ||
| 12 | --only-pacman clean only pacman cache | ||
| 13 | --only-aur clean only paru cache | ||
| 14 | -h, --help display this help message and exit | ||
| 15 | |||
| 16 | EXAMPLES: | ||
| 17 | $script_name # clean both pacman and paru cache | ||
| 18 | $script_name --only-pacman # clean only pacman cache | ||
| 19 | $script_name --only-aur # clean only paru cache | ||
| 20 | |||
| 21 | EOF | ||
| 22 | } | ||
| 23 | |||
| 24 | handle_interrupt() { | ||
| 25 | printf "\n%s: operation canceled by user\n" "$script_name" >&2 | ||
| 26 | exit 130 | ||
| 27 | } | ||
| 28 | |||
| 29 | trap handle_interrupt INT | ||
| 30 | |||
| 31 | error_exit() { | ||
| 32 | printf "%s: error: %s\n" "$script_name" "$1" >&2 | ||
| 33 | exit 1 | ||
| 34 | } | ||
| 35 | |||
| 36 | check_privilege() { | ||
| 37 | if [ "$(id -u)" -eq 0 ]; then | ||
| 38 | return 0 | ||
| 39 | fi | ||
| 40 | if ! command -v sudo >/dev/null 2>&1; then | ||
| 41 | error_exit "sudo is required but not found" | ||
| 42 | fi | ||
| 43 | } | ||
| 44 | |||
| 45 | # run command with elevated privileges only if needed | ||
| 46 | run_privileged() { | ||
| 47 | if [ "$(id -u)" -eq 0 ]; then | ||
| 48 | "$@" | ||
| 49 | else | ||
| 50 | sudo "$@" | ||
| 51 | fi | ||
| 52 | } | ||
| 53 | |||
| 54 | # clean pacman cache | ||
| 55 | clean_pacman_cache() { | ||
| 56 | printf "cleaning pacman cache...\n" | ||
| 57 | run_privileged paccache --remove --keep 0 || error_exit "failed to clean pacman cache" | ||
| 58 | } | ||
| 59 | |||
| 60 | # clean paru cache | ||
| 61 | clean_aur_cache() { | ||
| 62 | cache_dir="$HOME/.cache/paru/clone" | ||
| 63 | |||
| 64 | printf "cleaning paru cache...\n" | ||
| 65 | if [ -d "$cache_dir" ]; then | ||
| 66 | rm -rf "$cache_dir" || error_exit "failed to clean paru cache" | ||
| 67 | else | ||
| 68 | printf "warning: paru cache directory not found: %s\n" "$cache_dir" | ||
| 69 | fi | ||
| 70 | |||
| 71 | printf "regenerating paru database...\n" | ||
| 72 | paru --gendb || error_exit "failed to regenerate paru database" | ||
| 73 | } | ||
| 74 | |||
| 75 | # default values | ||
| 76 | clean_pacman=1 | ||
| 77 | clean_aur=1 | ||
| 78 | |||
| 79 | # parse args | ||
| 80 | while [ $# -gt 0 ]; do | ||
| 81 | case "$1" in | ||
| 82 | -h|--help) | ||
| 83 | show_help | ||
| 84 | exit 0 | ||
| 85 | ;; | ||
| 86 | --only-pacman) | ||
| 87 | clean_aur=0 | ||
| 88 | ;; | ||
| 89 | --only-aur) | ||
| 90 | clean_pacman=0 | ||
| 91 | ;; | ||
| 92 | *) | ||
| 93 | printf "%s: error: unknown option: %s\n" "$script_name" "$1" >&2 | ||
| 94 | printf "try '%s --help' for more information\n" "$script_name" >&2 | ||
| 95 | exit 1 | ||
| 96 | ;; | ||
| 97 | esac | ||
| 98 | shift | ||
| 99 | done | ||
| 100 | |||
| 101 | # checks | ||
| 102 | if [ "$clean_pacman" -eq 0 ] && [ "$clean_aur" -eq 0 ]; then | ||
| 103 | error_exit "cannot use --only-pacman and --only-aur together" | ||
| 104 | fi | ||
| 105 | |||
| 106 | if [ "$clean_pacman" -eq 1 ]; then | ||
| 107 | command -v paccache >/dev/null 2>&1 || error_exit "paccache is required but not found (install pacman-contrib)" | ||
| 108 | check_privilege | ||
| 109 | fi | ||
| 110 | |||
| 111 | if [ "$clean_aur" -eq 1 ]; then | ||
| 112 | command -v paru >/dev/null 2>&1 || error_exit "paru is required but not found" | ||
| 113 | fi | ||
| 114 | |||
| 115 | # clean relevant caches | ||
| 116 | [ "$clean_pacman" -eq 1 ] && clean_pacman_cache | ||
| 117 | [ "$clean_aur" -eq 1 ] && clean_aur_cache | ||
| 118 | |||
| 119 | printf "cleanup complete!\n" \ No newline at end of file | ||
