-
Notifications
You must be signed in to change notification settings - Fork 31
/
fzaur
executable file
·130 lines (114 loc) · 3.29 KB
/
fzaur
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
#!/usr/bin/env bash
declare -A aliases
declare -A helptext
declare -r c_reset=$(tput sgr0)
declare -r c_red=$(tput setaf 1)
declare -r c_green=$(tput setaf 2)
declare -r c_yellow=$(tput setaf 3)
declare -r c_grey=$(tput setaf 8)
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
if (( $# > 0 )); then
err "$@"
fi
exit 1
}
has() {
local v c
if [[ $1 = '-v' ]]; then
v=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( v > 0 )) && err "$c not found"
return 1
fi
done
}
aliases[h]=help
aliases[-h]=help
aliases[--help]=help
helptext[help]='show this help'
subcmd_help() {
local formattedhelptext
formattedhelptext=$(for c in "${subcmds_avail[@]}"; do
printf " %s\n %s\n" "$c" "${helptext[$c]}"
done)
LESS=-FEXR less <<-HELP
$0 <COMMAND>
${formattedhelptext}
HELP
}
aliases[s]=search
aliases[-s]=search
helptext[search]='search packages on arch user repositories'
subcmd_search() {
local q="$*" choices
# local search_cmd='curl -s https://www.archlinux.org/packages/search/json/ | jq -r ".results[] | select(.Name | contains(\"{q}\")) | .Name"'
local url_search='https://aur.archlinux.org/rpc/v5/search/'
local jq_package_listing=".results | sort_by(.NumVotes) | reverse[] | \"${c_yellow}\(.Name)${c_grey} - \(if .OutOfDate then \"${c_red}[out of date] \" else \"\" end)${c_green}▲\(.NumVotes)${c_grey} - ${c_reset}\(.Description)\""
local url_info='https://aur.archlinux.org/rpc/v5/info?arg[]='
local jq_package_info='.results[]'
local cmd_search="curl -sL '${url_search}{q}' | jq -r '$jq_package_listing'"
mapfile -t choices < <(fzf -m --phony --query="$q" \
--preview="curl -sL '${url_info}{1}' | jq -C '$jq_package_info'" \
--bind=change:"reload(${cmd_search})" \
--bind=start:"reload(${cmd_search})" | awk '{print $1}')
(( ${#choices[@]} < 1 )) && exit 0
clone_package "${choices[0]}"
}
clone_package() {
local pkg="$1" url
if [[ $pkg != http* ]]; then
url="https://aur.archlinux.org/${pkg}"
fi
# TODO: make this configurable
local clonedir=~/.cache/fzaur
if [[ ! -d "$clonedir/$pkg" ]]; then
mkdir -p "$clonedir"
fi
cd "$clonedir" || die "failed to mkdir $clonedir"
if [[ -d "$pkg" ]]; then
cd "$pkg" 2> /dev/null || die 'failed to cd into package'
if [[ ! -d .git ]]; then
die "$clonedir/$pkg already exists but is not a git repo"
fi
git pull
else
git clone "$url" || die 'failed to clone package'
cd "$pkg" 2> /dev/null || die 'failed to clone package'
fi
$EDITOR $PWD/PKGBUILD
read -r -n 1 -p "press y to run ${c_green}makepkg -si${c_reset} or any other key to abort: "
[[ $REPLY != y ]] && exit 1
makepkg -si
}
has -v fzf jq curl || die
mapfile -t subcmds_avail < <(compgen -A function | awk '/^subcmd_/ { sub(/^subcmd_/, "", $0); print }')
nocmd() {
local cmd=$(for c in "${subcmds_avail[@]}"; do
printf "$c\t${help}\t${helptext[$c]}\n"
done)
local choice=$(<<< "$cmd" column -t -s $'\t' | fzf | awk '{print $1}')
if [[ -z $choice ]]; then exit 1; fi
subcmd_$choice
}
if (( $# < 1 )); then
nocmd
exit 1
elif has "subcmd_$1"; then
subcmd="subcmd_$1"
shift
"$subcmd" "$@"
elif [[ -v aliases[$1] ]]; then
subcmd=subcmd_${aliases[$1]}
shift
"$subcmd" "$@"
else
echo "unknown command \"$1\""
subcmd_help
exit 1
fi