-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacb
executable file
·207 lines (172 loc) · 4.51 KB
/
pacb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
# shellcheck disable=SC2086
set -Euo pipefail
_DEBUGPIPE=''
CLEAN=0
REFRESH=1
EXACT="--exact"
FUZZER=""
if [[ -f "$HOME/.pkgbdebug" ]]; then
_DEBUGPIPE=$(mktemp)
echo "Started in Debug mode!"
echo "Debug pipe: $_DEBUGPIPE"
_trace_handler() {
if [[ ! -p $_DEBUGPIPE ]]; then
mkfifo "$_DEBUGPIPE"
fi
echo -e "\n[DBG]➜\t$BASH_COMMAND" &>"$_DEBUGPIPE"
}
set -o functrace
trap '_trace_handler' DEBUG
fi
_fail() {
printf '\e[0m'
printf '\n\e[38;2;200;0;0m%s\e[0m\n' "FAIL: $1"
exit 1
}
_info() {
printf '\e[0m'
printf '\e[38;2;0;200;0m%s\e[0m\n' "$1"
}
_terminate_handler() {
_fail "Caught terminate signal"
}
_error_handler() {
ORIGIN=$1
ERRNO=$2
printf '\e[0m'
printf '\n\e[1m\e[48;2;236;240;241m\e[38;2;200;0;0m %s \e[0m' "!! > Internal failure. Command '$ORIGIN' failed with $ERRNO"
printf '\n'
exit 1
}
_installed() {
local result
command -v "$1" &> /dev/null
result=$?
return $result
}
trap '_terminate_handler' SIGINT SIGTERM
trap '_error_handler $BASH_COMMAND $?' ERR
ACTIONS=('add' 'del' 'export' 'list' 'upgrade' 'addgroup' 'browse')
INFO_HEADER="<ESC> Exit, <PgUp> Preview up, <PgDown> Preview down <F5> Apply"
SUDO_CMD=$(command -v sudo || echo '')
if [[ -z "$SUDO_CMD" ]]; then
if [[ $EUID -ne 0 ]]; then
_fail 'sudo not found and current user is not root'
fi
fi
_get_installed_pkgs() {
pacman -Qq
}
_get_groups() {
pacman -Qg | awk '{if (a[$1]++ == 0){print $1}}'
}
_pkgb_clean() {
$SUDO_CMD pacman -Rns "$(pacman -Qtdq)"
$SUDO_CMD pacman -Sc
}
_pkgb_refresh() {
if [[ $REFRESH = 1 ]]; then
$SUDO_CMD pacman -Sy
fi
}
pkgb_addgroup() {
local groups
local selection
groups=$(_get_groups)
selection=$(echo "$groups" | tr ' ' '\n' | sort | uniq -u | "$FUZZER" "${FUZZER_OPTS[@]}" --query="$1" --preview='pacman -Si {}' --prompt='Install packages > ' --bind "F5:accept" || echo '')
if [[ -n "$selection" ]]; then
cmd=$(echo "$selection" | tr '\n' ' ')
"$SUDO_CMD" pacman -Syu $cmd
fi
}
pkgb_add() {
local installed_pkg
local all_pkg
local selection
installed_pkg=$(_get_installed_pkgs)
all_pkg=$(pacman -Slq)
selection=$(echo "$installed_pkg" "$all_pkg" | tr ' ' '\n' | sort | uniq -u | "$FUZZER" "${FUZZER_OPTS[@]}" --query="$1" --preview='pacman -Si {}' --prompt='Install packages > ' --bind "F5:accept" || echo '')
if [[ -n "$selection" ]]; then
cmd=$(echo "$selection" | tr '\n' ' ')
"$SUDO_CMD" pacman -Syu $cmd
fi
}
pkgb_browse() {
local installed_pkg
local all_pkg
local selection
all_pkg=$(pacman -Slq)
echo "$all_pkg" | tr ' ' '\n' | "$FUZZER" "${FUZZER_OPTS[@]}" --query="$1" --preview='pacman -Si {}' --prompt='Packages > ' --bind "F5:accept" || echo ''
}
pkgb_del() {
local selection
selection=$(_get_installed_pkgs | "$FUZZER" "${FUZZER_OPTS[@]}" --preview='pacman -Qil {}' --query="$1" --prompt='Uninstall packages > ' || echo '')
if [[ -n "$selection" ]]; then
cmd=$(echo "$selection" | tr '\n' ' ')
"$SUDO_CMD" pacman -Rs $cmd
fi
}
pkgb_list() {
_get_installed_pkgs | "$FUZZER" --preview='pacman -Qil {}' --prompt='Installed packages > ' --query="$1" || true
}
pkgb_export() {
_get_installed_pkgs
}
pkgb_upgrade() {
$SUDO_CMD pacman -Syu
}
pkgb_help() {
echo "Usage: $(basename "$0") [-c] [-u] [-h] (${ACTIONS[*]}) [QUERY]" >&2
}
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts 'chu' OPTION; do
case "$OPTION" in
c)
_info "-> Requested autoclean"
CLEAN=1
;;
u)
_info "-> Disabled exact mode"
EXACT=""
;;
h)
pkgb_help
exit 1
;;
*)
echo "Invalid option"
pkgb_help
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [[ $# -lt 1 ]]; then
echo "No action specified"
pkgb_help
exit 1
fi
if _installed "sk"; then
FUZZER="sk"
echo "Using sk"
elif _installed "fzf"; then
FUZZER="fzf"
echo "Using fzf"
else
_fail "No fuzzy matcher installed. Please install fzf or sk!"
fi
FUZZER_OPTS=("--no-height" "--multi" "--bind" "pgdn:preview-down" "$EXACT" "--bind" "pgup:preview-up" "--header" "$INFO_HEADER")
for a in "${ACTIONS[@]}"; do
if [ "$a" == "$1" ]; then
shift
"pkgb_${a}" "$*"
if [[ $CLEAN = 1 ]]; then
_pkgb_clean
fi
exit 0
fi
done
echo "Unknown Action: $1"
pkgb_help
exit 1