/
task-blocks
executable file
·132 lines (119 loc) · 2.7 KB
/
task-blocks
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
#!/bin/bash
set -eu
EXEC=${0##*/}
if [[ $EXEC = on-launch.* ]]; then
HOOK=on-launch
elif [[ $EXEC = on-exit.* ]]; then
HOOK=on-exit
elif [[ $EXEC = on-modify.* ]]; then
HOOK=on-modify
read -r OLD
read -r NEW
elif [[ $EXEC = on-add.* ]]; then
HOOK=on-add
read -r NEW
else
echo "Unknown hook: $EXEC"
exit 1
fi
join() {
local IFS="$1"
shift
echo "$*"
}
task() {
command task rc.confirmation=no rc.bulk=10000 rc.verbose=nothing "$@"
}
block() {
local UUID=$1
local BLOCKS=$2
if [[ $BLOCKS = -* ]]; then
UUID="-$UUID"
BLOCKS=${BLOCKS#*-}
fi
task "$BLOCKS" modify depends:"$UUID" >&2 || {
echo "Task $BLOCKS could not be blocked"
return 1
}
}
printline() {
printf "%s\n" "$1"
}
case $HOOK in
on-exit)
RES=0
while read -r NEW; do
if [[ "$NEW" = *\"blocks\"* ]] && JQ=$(jq -Mcre '.blocks, .uuid' <<< "$NEW"); then
{
read -r BLOCKS
read -r UUID
task "$UUID" modify blocks: >&2 || RES=$?
IFS=, BLOCKS_ARR=($BLOCKS)
for BLOCK in ${BLOCKS_ARR[@]+"${BLOCKS_ARR[@]}"}; do
block "$UUID" "$BLOCK" || RES=$?
done
} <<< "$JQ"
fi
done
exit $RES
;;
on-modify|on-add)
if [[ "$NEW" = *\"blocked\"* ]] && JQ=$(jq -Mcre '.blocked, .depends // ""' <<< "$NEW"); then
{
read -r BLOCKED
read -r DEPENDS || DEPENDS=
FILTER=()
if [[ $DEPENDS = [* ]]; then
DEPENDS_ARR=($(jq -Mcre '.[]' <<< "$DEPENDS"))
DEPENDS=$(join , ${DEPENDS_ARR[@]+"${DEPENDS_ARR[@]}"})
fi
if [[ -n $BLOCKED ]]; then
IFS=, DEPENDS_ARR=($DEPENDS)
IFS=, BLOCKED_ARR=($BLOCKED)
for BLOCK in ${BLOCKED_ARR[@]+"${BLOCKED_ARR[@]}"}; do
NEG=false
if [[ $BLOCK = -* ]]; then
NEG=true
BLOCK=${BLOCK#*-}
fi
UUID=($(task "$BLOCK" _unique uuid))
if [[ ${#UUID[@]} -eq 1 ]]; then
if $NEG; then
FILTER+=("${UUID[0]}")
else
DEPENDS_ARR+=("${UUID[0]}")
fi
elif [[ ${#UUID[@]} -eq 0 ]]; then
echo "blocked: no task matching \"$BLOCK\""
exit 1
else
echo "blocked: more than one match for \"$BLOCK\": ${UUID[*]}"
exit 1
fi
done
DEPENDS_FILTERED=()
for UUID in ${DEPENDS_ARR[@]+"${DEPENDS_ARR[@]}"}; do
FILTERED=false
for FUUID in ${FILTER[@]+"${FILTER[@]}"}; do
if [[ $UUID = $FUUID ]]; then
FILTERED=true
fi
done
if ! $FILTERED; then
DEPENDS_FILTERED+=("$UUID")
fi
done
DEPENDS=$(join , ${DEPENDS_FILTERED[@]+"${DEPENDS_FILTERED[@]}"})
fi
if [[ -n $DEPENDS ]]; then
QUERY="del(.blocked) + {\"depends\": \"$DEPENDS\"}"
else
QUERY="del(.blocked, .depends)"
fi
jq -Mce "$QUERY" <<< "$NEW"
} <<< "$JQ"
else
printline "$NEW"
fi
;;
esac