forked from J-Carder/waybar-apt-updates
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinput-checker
More file actions
executable file
·285 lines (241 loc) · 9.82 KB
/
Copy pathinput-checker
File metadata and controls
executable file
·285 lines (241 loc) · 9.82 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
# Flake Input Checker for Waybar
# Compares locked flake input revisions against upstream using git ls-remote.
# Runs in seconds — no nix evaluation or builds required.
#
# Checks each GitHub input in flake.lock against its tracked branch.
# Supports both unpinned and pinned inputs with configurable modes.
# ===== Configuration =====
FLAKE_DIR="${FLAKE_DIR:-$HOME/.config/nixos}"
UPDATE_INTERVAL="${UPDATE_INTERVAL:-3599}"
CACHE_DIR="${CACHE_DIR:-$HOME/.cache}"
# Mode: "disabled" | "show" | "count"
# - disabled: Don't check inputs
# - show: Check and show in tooltip, but don't include in count
# - count: Check, show in tooltip, and include in waybar count
INPUT_CHECKER_MODE="${INPUT_CHECKER_MODE:-count}"
INPUT_CHECKER_PINNED="${INPUT_CHECKER_PINNED:-disabled}"
# The INPUT_UPDATE_FLAG signals that flake inputs were updated (e.g. nix flake update).
# Forces an immediate re-check. Add this to any alias that runs nix flake update:
# touch "$HOME/.cache/nix-update-input-update-flag" && pkill -x -RTMIN+12 .waybar-wrapped
INPUT_UPDATE_FLAG="$CACHE_DIR/nix-update-input-update-flag"
STATE_FILE="$CACHE_DIR/nix-update-input-state"
LAST_RUN_FILE="$CACHE_DIR/nix-update-input-last-run"
LAST_RUN_TOOLTIP="$CACHE_DIR/nix-update-input-tooltip"
INPUT_STATE_FILE="$CACHE_DIR/nix-update-input-state"
INPUT_TOOLTIP_FILE="$CACHE_DIR/nix-update-input-tooltip"
PINNED_STATE_FILE="$CACHE_DIR/nix-update-pinned-state"
PINNED_TOOLTIP_FILE="$CACHE_DIR/nix-update-pinned-tooltip"
# ===== Initialize =====
mkdir -p "$CACHE_DIR"
[ -f "$STATE_FILE" ] || echo "0" > "$STATE_FILE"
[ -f "$LAST_RUN_FILE" ] || echo "0" > "$LAST_RUN_FILE"
[ -f "$LAST_RUN_TOOLTIP" ] || echo "All inputs up to date" > "$LAST_RUN_TOOLTIP"
output_json() {
echo "{ \"text\":\"$1\", \"alt\":\"$2\", \"tooltip\":\"$3\" }"
}
check_unpinned_inputs() {
local flake_lock="$FLAKE_DIR/flake.lock"
[[ ! -f "$flake_lock" ]] && return 1
local count=0
local tooltip=""
# Check GitHub inputs (unpinned)
while IFS=$'\t' read -r name owner repo rev ref last_modified; do
local remote_ref
[[ "$ref" == "HEAD" ]] && remote_ref="HEAD" || remote_ref="refs/heads/$ref"
local latest
latest=$(GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="" git ls-remote \
"https://github.com/$owner/$repo" "$remote_ref" 2>/dev/null | cut -f1)
if [[ -n "$latest" && "$latest" != "$rev" ]]; then
count=$((count + 1))
local locked_date
locked_date=$(date -d "@$last_modified" +%Y-%m-%d 2>/dev/null || echo "?")
[[ -n "$tooltip" ]] && tooltip+="\\n"
tooltip+="$name (locked: $locked_date)"
fi
done < <(jq -r '
. as $root |
$root.nodes.root.inputs | to_entries[] |
select(.value | type == "string") |
{name: .key, node: $root.nodes[.value]} |
select(.node.locked.type == "github") |
select(.node.original.rev == null) |
[.name, .node.original.owner, .node.original.repo, .node.locked.rev,
(.node.original.ref // "HEAD"), (.node.locked.lastModified | tostring)] |
@tsv
' "$flake_lock")
# Check generic git inputs (Bitbucket, GitLab, self-hosted, etc.) - unpinned
while IFS=$'\t' read -r name url rev ref last_modified; do
local remote_ref="$ref"
local latest
latest=$(GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="" git ls-remote \
"$url" "$remote_ref" 2>/dev/null | cut -f1)
if [[ -n "$latest" && "$latest" != "$rev" ]]; then
count=$((count + 1))
local locked_date
locked_date=$(date -d "@$last_modified" +%Y-%m-%d 2>/dev/null || echo "?")
[[ -n "$tooltip" ]] && tooltip+="\\n"
tooltip+="$name (locked: $locked_date)"
fi
done < <(jq -r '
. as $root |
$root.nodes.root.inputs | to_entries[] |
select(.value | type == "string") |
{name: .key, node: $root.nodes[.value]} |
select(.node.locked.type == "git") |
select(.node.original.rev == null) |
[.name, .node.locked.url, .node.locked.rev,
(.node.locked.ref // "HEAD"), (.node.locked.lastModified | tostring)] |
@tsv
' "$flake_lock")
echo "$count" > "$INPUT_STATE_FILE"
if [ "$count" -eq 0 ]; then
echo "" > "$INPUT_TOOLTIP_FILE"
else
echo "$tooltip" > "$INPUT_TOOLTIP_FILE"
fi
return 0
}
check_pinned_inputs() {
local flake_lock="$FLAKE_DIR/flake.lock"
[[ ! -f "$flake_lock" ]] && return 1
local count=0
local tooltip=""
# Check pinned GitHub inputs
while IFS=$'\t' read -r name owner repo rev ref last_modified; do
local remote_ref
[[ "$ref" == "HEAD" ]] && remote_ref="HEAD" || remote_ref="refs/heads/$ref"
local latest
latest=$(GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="" git ls-remote \
"https://github.com/$owner/$repo" "$remote_ref" 2>/dev/null | cut -f1)
if [[ -n "$latest" && "$latest" != "$rev" ]]; then
count=$((count + 1))
local locked_date
locked_date=$(date -d "@$last_modified" +%Y-%m-%d 2>/dev/null || echo "?")
[[ -n "$tooltip" ]] && tooltip+="\\n"
tooltip+="$name (pinned: $locked_date)"
fi
done < <(jq -r '
. as $root |
$root.nodes.root.inputs | to_entries[] |
select(.value | type == "string") |
{name: .key, node: $root.nodes[.value]} |
select(.node.locked.type == "github") |
select(.node.original.rev != null) |
[.name, .node.original.owner, .node.original.repo, .node.locked.rev,
(.node.original.ref // "HEAD"), (.node.locked.lastModified | tostring)] |
@tsv
' "$flake_lock")
# Check pinned generic git inputs
while IFS=$'\t' read -r name url rev ref last_modified; do
local remote_ref="$ref"
local latest
latest=$(GIT_TERMINAL_PROMPT=0 GIT_ASKPASS="" git ls-remote \
"$url" "$remote_ref" 2>/dev/null | cut -f1)
if [[ -n "$latest" && "$latest" != "$rev" ]]; then
count=$((count + 1))
local locked_date
locked_date=$(date -d "@$last_modified" +%Y-%m-%d 2>/dev/null || echo "?")
[[ -n "$tooltip" ]] && tooltip+="\\n"
tooltip+="$name (pinned: $locked_date)"
fi
done < <(jq -r '
. as $root |
$root.nodes.root.inputs | to_entries[] |
select(.value | type == "string") |
{name: .key, node: $root.nodes[.value]} |
select(.node.locked.type == "git") |
select(.node.original.rev != null) |
[.name, .node.locked.url, .node.locked.rev,
(.node.locked.ref // "HEAD"), (.node.locked.lastModified | tostring)] |
@tsv
' "$flake_lock")
echo "$count" > "$PINNED_STATE_FILE"
if [ "$count" -eq 0 ]; then
echo "" > "$PINNED_TOOLTIP_FILE"
else
echo "$tooltip" > "$PINNED_TOOLTIP_FILE"
fi
return 0
}
run_checks() {
local input_count=0
local input_tooltip=""
local input_count_for_total=0
local pinned_count=0
local pinned_tooltip=""
local pinned_count_for_total=0
# Check unpinned inputs based on mode
if [ "$INPUT_CHECKER_MODE" != "disabled" ]; then
check_unpinned_inputs
input_count=$(cat "$INPUT_STATE_FILE" 2>/dev/null || echo "0")
input_tooltip=$(cat "$INPUT_TOOLTIP_FILE" 2>/dev/null || echo "")
if [ "$INPUT_CHECKER_MODE" = "count" ]; then
input_count_for_total=$input_count
fi
fi
# Check pinned inputs based on mode
if [ "$INPUT_CHECKER_PINNED" != "disabled" ]; then
check_pinned_inputs
pinned_count=$(cat "$PINNED_STATE_FILE" 2>/dev/null || echo "0")
pinned_tooltip=$(cat "$PINNED_TOOLTIP_FILE" 2>/dev/null || echo "")
if [ "$INPUT_CHECKER_PINNED" = "count" ]; then
pinned_count_for_total=$pinned_count
fi
fi
# Calculate total count
local total_count=$((input_count_for_total + pinned_count_for_total))
echo "$total_count" > "$STATE_FILE"
echo "$(date +%s)" > "$LAST_RUN_FILE"
# Build tooltip
local final_tooltip=""
if [ "$INPUT_CHECKER_MODE" != "disabled" ] && [ "$input_count" -gt 0 ]; then
if [ "$INPUT_CHECKER_PINNED" != "disabled" ]; then
final_tooltip="Inputs:\\n$input_tooltip"
else
final_tooltip="$input_tooltip"
fi
fi
if [ "$INPUT_CHECKER_PINNED" != "disabled" ] && [ "$pinned_count" -gt 0 ]; then
if [ -n "$final_tooltip" ]; then
final_tooltip="$final_tooltip\\n\\nPinned:\\n$pinned_tooltip"
else
final_tooltip="Pinned:\\n$pinned_tooltip"
fi
fi
if [ -z "$final_tooltip" ]; then
echo "All inputs up to date" > "$LAST_RUN_TOOLTIP"
else
echo "$final_tooltip" > "$LAST_RUN_TOOLTIP"
fi
return 0
}
# ===== Main =====
updates=$(cat "$STATE_FILE" 2>/dev/null || echo "0")
last_run=$(cat "$LAST_RUN_FILE" 2>/dev/null || echo "0")
current_time=$(date +%s)
# Check if input checking is completely disabled
if [ "$INPUT_CHECKER_MODE" = "disabled" ] && [ "$INPUT_CHECKER_PINNED" = "disabled" ]; then
output_json "0" "updated" "Input checking disabled"
exit 0
fi
# If flake inputs were updated, force a re-check regardless of interval
if [ -f "$INPUT_UPDATE_FLAG" ]; then
rm -f "$INPUT_UPDATE_FLAG"
rm -f "$LAST_RUN_FILE"
last_run=0
fi
if [ $((current_time - last_run)) -gt "$UPDATE_INTERVAL" ]; then
if run_checks; then
updates=$(cat "$STATE_FILE" 2>/dev/null || echo "0")
else
output_json "?" "error" "Input check failed"
exit 0
fi
fi
if [ "$updates" -ne 0 ] 2>/dev/null; then
tooltip=$(cat "$LAST_RUN_TOOLTIP" 2>/dev/null || echo "$updates inputs behind")
output_json "$updates" "has-updates" "$tooltip"
else
output_json "0" "updated" "All inputs up to date"
fi