repo: prefix pacman config in errors
[aurutils.git] / lib / aur-repo
blobecbeac33b83a9e4d421b409edf8517ea34e9ba53
1 #!/bin/bash
2 # aur-repo - operate on local repositories
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 argv0=repo
5 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
7 # default options
8 db_query=file mode=none conf_args=() vercmp_args=() parse_args=()
10 args_csv() {
11 # shellcheck disable=SC2155
12 local str=$(printf '%s,' "$@")
13 printf '%s' "${str%,}"
16 usage() {
17 printf >&2 'usage: %s [-d repo] [-r path] [-alqtuS]\n' "$argv0"
18 exit 1
21 # option parsing
22 opt_short='c:f:d:r:s:F:alqtuJS'
23 opt_long=('config:' 'database:' 'root:' 'all' 'list' 'path' 'list-path' 'list-repo'
24 'search:' 'search-by:' 'list-attr' 'sync' 'upgrades' 'table' 'quiet'
25 'attr:' 'json' 'jsonl' 'format:' 'delim:' 'dbext:')
26 opt_hidden=('dump-options' 'repo:' 'repo-list' 'path-list' 'attr-list' 'status' 'field:')
28 if opts=$(getopt -o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
29 eval set -- "$opts"
30 else
31 usage
34 unset mode list db_ext db_name db_root format status_file pacman_conf format_args vercmp_args attr
35 while true; do
36 case $1 in
37 -d|--database|--repo)
38 shift; db_name=$1 ;;
39 -f|--format)
40 shift; format=$1; mode=format ;;
41 --delim)
42 shift; format_args+=(--delim "$1") ;;
43 -r|--root)
44 shift; db_root=$1 ;;
45 --dbext)
46 shift; db_ext=$1 ;;
47 -c|--config)
48 shift; pacman_conf=$1; conf_args+=(--config "$1") ;;
49 -l|--list)
50 mode=list ;;
51 -t|--table)
52 mode=table ;;
53 -J|--json)
54 mode=json ;;
55 --jsonl)
56 mode=jsonl ;;
57 -a|--all)
58 mode=upgrades; vercmp_args+=(-a) ;;
59 -u|--upgrades)
60 mode=upgrades ;;
61 -q|--quiet)
62 parse_args+=(-q); vercmp_args+=(-q) ;;
63 -S|--sync)
64 db_query=sync ;;
65 -F|--attr|--field)
66 shift; mode=attr; attr=$1 ;;
67 -s|--search)
68 shift; parse_args+=(--search "$1") ;;
69 --search-by)
70 shift; parse_args+=(--search-by "$1") ;;
71 --path)
72 mode=path ;;
73 --status)
74 mode=status ;;
75 --path-list|--list-path)
76 list=path ;;
77 --repo-list|--list-repo)
78 list=repo ;;
79 --attr-list|--list-attr)
80 list=attr ;;
81 --dump-options)
82 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
83 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
84 exit ;;
85 --) shift; break ;;
86 esac
87 shift
88 done
90 # assign environment variables
91 db_ext=${db_ext:-$AUR_DBEXT} db_name=${db_name:-$AUR_REPO} db_root=${db_root:-$AUR_DBROOT}
93 # option sanity check
94 if [[ $db_query == 'sync' ]] && [[ $db_root ]]; then
95 printf >&2 'warning: ignoring repository root %q with --sync\n' "$db_root"
98 # parse pacman configuration
99 declare -A conf_file_serv conf_file_path conf_sync_serv
101 while read -r key _ value; do
102 case $key=$value in
103 \[*\]*)
104 section=${key:1:-1}
106 DBPath=*)
107 pacman_dbpath=${value%/}
109 Server=file://*)
110 value=${value#file://}
112 conf_file_serv[$section]=$value
113 conf_file_path[$section]=$value/$section.${db_ext:-db}
115 Server=*://*)
116 conf_sync_serv[$section]=$value
118 if [[ $section == "$db_name" ]] && [[ ! $db_root ]]; then
119 db_root=$value
120 fi ;;
121 esac
122 done < <(pacman-conf "${conf_args[@]}")
123 wait $! || exit
125 # list information on available local repositories
126 case $list in
127 path|repo)
128 if ! [[ ${!conf_file_path[*]} ]]; then
129 printf >&2 '%s: no file:// repository configured\n' "$argv0"
130 exit 2
131 fi ;;&
132 path)
133 # XXX: file paths not quoted
134 realpath -- "${conf_file_path[@]}" # resolve repo-add symlinks
135 exit 0 ;;
136 repo)
137 printf '%q\n' "${!conf_file_path[@]}"
138 exit 0 ;;
139 attr)
140 aur repo-parse --list-attr # holds for any repository
141 exit ;;
142 esac
144 # select local repository from pacman configuration, if no repository
145 # was specified on the command-line
146 if [[ ! $db_name ]]; then
147 conf_file_repo=("${!conf_file_serv[@]}")
149 case ${#conf_file_repo[@]} in
150 1) db_root=${conf_file_serv[${conf_file_repo[0]}]}
151 db_name=${conf_file_repo[0]}
153 0) printf >&2 '%s: no file:// repository configured\n' "$argv0"
154 exit 2
156 *) printf >&2 '%s: repository choice is ambiguous (use -d to specify)\n' "$argv0"
158 for db in "${conf_file_repo[@]}"; do
159 printf '%q\t%q\n' "$db" "${conf_file_path[$db]}"
160 done | column -o $'\t' -t >&2
161 exit 1
163 esac
165 # check $db_name is a configured pacman repository (local or remote, #1113)
166 elif ! [[ ${conf_sync_serv[$db_name]} ]]; then
167 # disambiguate used pacman.conf (#1118)
168 if [[ -v pacman_conf ]]; then
169 printf >&2 "%s: %q: repository %q not configured\n" "$argv0" "$pacman_conf" "$db_name"
170 else
171 printf >&2 "%s: repository %q not configured\n" "$argv0" "$db_name"
173 exit 2
176 # basic file checks
177 case $db_query in
178 file)
179 if [[ ! $db_root ]]; then
180 printf >&2 '%s: %q: repository root not found\n' "$argv0" "$db_name"
181 exit 2
182 elif [[ $db_root == *://* ]]; then
183 printf >&2 '%s: %q: object is remote (use -S to query)\n' "$argv0" "$db_root"
184 exit 66
186 db_path=$db_root/$db_name.${db_ext:-db}
188 sync)
189 db_path=$pacman_dbpath/sync/$db_name.${db_ext:-db}
190 db_root=$pacman_dbpath/sync
192 esac
194 # check file attributes
195 if [[ ! -d $db_root ]]; then
196 printf >&2 '%s: %q: no such directory\n' "$argv0" "$db_root"
197 exit 2
198 elif [[ ! -f $db_path ]]; then
199 printf >&2 '%s: %q: no such file\n' "$argv0" "$db_path"
200 exit 2
203 # resolve repo-add symlink
204 db_path=$(realpath -- "$db_path")
205 db_root=$(realpath -- "$db_root")
207 # database operations
208 case $mode in
209 list|table|json|jsonl)
210 aur repo-parse "${parse_args[@]}" -p "$db_path" --"$mode"
212 format)
213 aur repo-parse "${parse_args[@]}" -p "$db_path" --jsonl | aur format "${format_args[@]}" -f "$format"
215 upgrades)
216 aur repo-parse "${parse_args[@]}" -p "$db_path" --list | aur vercmp "${vercmp_args[@]}"
218 attr)
219 aur repo-parse "${parse_args[@]}" -p "$db_path" --attr "$attr"
221 path)
222 printf '%q\n' "$db_path"
225 printf 'repo:%q\nroot:%q\npath:%q\n' "$db_name" "$db_root" "$db_path"
227 esac
229 # vim: set et sw=4 sts=4 ft=sh: