-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-query
More file actions
executable file
·63 lines (52 loc) · 1.28 KB
/
Copy pathlist-query
File metadata and controls
executable file
·63 lines (52 loc) · 1.28 KB
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
#!/bin/sh
set -eu
strip_uuid() {
if [ "${strip_uuid}" = 'true' ]; then
sed -E '/ uuid:/ s/ uuid:\S+//'
else
cat
fi
}
filter() {
if [ "$#" -eq 0 ]; then
cat
return $?
fi
insensitive=
"${case_sensitive}" || insensitive='tolower($0) ~ '
case "${filter_mode}" in
all)
regex=$(printf "${insensitive}/%s/ && " "$@" | sed 's/ && $//')
;;
any)
regex=$(printf "${insensitive}/%s/ || " "$@" | sed 's/ || $//')
;;
esac
"${case_sensitive}" || regex=$(printf '%s\n' "${regex}" | tr '[:upper:]' '[:lower:]')
awk "${regex}"
}
usage() {
cat >&2 <<EOF
usage: list-query [-Uai] [-c context] [-f file] [-p project] [query ...]
EOF
exit 64 # EX_USAGE
}
filter_mode=all
case_sensitive=true
strip_uuid=true
file=/dev/stdin
contexts=
projects=
while getopts :Uaif:c:p: arg >/dev/null; do
case "${arg}" in
c) contexts="${contexts:+${contexts} }@${OPTARG}" ;;
f) file="${OPTARG}" ;;
p) projects="${projects:+${projects} }+${OPTARG}" ;;
U) strip_uuid=false ;;
a) filter_mode=any ;;
i) case_sensitive=false ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
filter "${contexts}" "${projects}" "$@" <"${file}" | strip_uuid