-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui
More file actions
executable file
·385 lines (336 loc) · 10.3 KB
/
Copy pathtui
File metadata and controls
executable file
·385 lines (336 loc) · 10.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env bash
#
# Pelotero Engine TUI
#
# Two modes:
# ./tui --build-all CI-friendly: nix-build every cabal executable.
# No gum, no dev shell, no postgres required.
# ./tui Interactive menu. Requires gum (use inside `nix develop`).
#
# Interactive mode wraps the `pelotero` CLI with prompts for the seasons,
# dates, and league ids you'd otherwise have to type by hand.
#
# The executable list is discovered from the .cabal file so adding a new
# executable later requires no change here.
set -uo pipefail
# ----------------------------------------------------------------------
# Project discovery
# ----------------------------------------------------------------------
CABAL_FILE="$(find . -maxdepth 1 -name '*.cabal' -print -quit)"
if [ -z "${CABAL_FILE:-}" ]; then
echo "No .cabal file in $(pwd)" >&2
exit 1
fi
PROJECT_NAME="$(grep -i '^name:' "$CABAL_FILE" | head -n1 | awk '{print $2}')"
if [ -z "$PROJECT_NAME" ]; then
echo "No 'name:' field in $CABAL_FILE" >&2
exit 1
fi
EXECUTABLES=()
while IFS= read -r line; do
exe="$(echo "$line" | awk '{print $2}')"
[ -n "$exe" ] && EXECUTABLES+=("$exe")
done < <(grep -E '^executable[[:space:]]+' "$CABAL_FILE")
if [ "${#EXECUTABLES[@]}" -eq 0 ]; then
echo "No executables in $CABAL_FILE" >&2
exit 1
fi
# ----------------------------------------------------------------------
# Build-all (CI mode, no gum needed)
# ----------------------------------------------------------------------
build_all() {
echo "Project: $PROJECT_NAME"
echo "Executables: ${EXECUTABLES[*]}"
echo ""
local failed=0
for exe in "${EXECUTABLES[@]}"; do
echo "==> nix build .#${PROJECT_NAME}:exe:${exe}"
if ! nix build ".#${PROJECT_NAME}:exe:${exe}" --show-trace --accept-flake-config; then
echo " FAILED: $exe"
failed=$((failed + 1))
fi
echo ""
done
if [ "$failed" -gt 0 ]; then
echo "$failed executable(s) failed to build."
exit 1
fi
echo "All builds succeeded."
}
case "${1:-}" in
--build-all)
build_all
exit 0
;;
--help|-h)
cat <<EOF
Usage:
$0 Interactive menu (requires gum; use inside 'nix develop')
$0 --build-all Build all executables via nix (CI-friendly)
$0 --help Show this help
Project: $PROJECT_NAME
Executables discovered: ${EXECUTABLES[*]}
EOF
exit 0
;;
esac
# ----------------------------------------------------------------------
# Interactive mode (gum required)
# ----------------------------------------------------------------------
if ! command -v gum >/dev/null 2>&1; then
cat >&2 <<EOF
gum is not on PATH.
Either:
- run this inside 'nix develop' (gum is in the dev shell), or
- use './tui --build-all' for non-interactive builds.
EOF
exit 1
fi
# ----- gum helpers -----
header() {
clear
gum style \
--foreground 10 --border-foreground 2 --border double \
--align center --width 64 --margin "1 2" --padding "1 3" \
"$(echo "$PROJECT_NAME" | tr '[:lower:]' '[:upper:]')" "fantasy baseball engine"
echo ""
}
section() {
gum style --foreground 6 --bold --margin "0 2" "$1"
echo ""
}
ok() { gum style --foreground 2 " ok $*"; }
err() { gum style --foreground 1 " fail $*"; }
info() { gum style --foreground 3 " .. $*"; }
dim() { gum style --foreground 8 " $*"; }
pause() {
echo ""
read -r -p " press enter to continue..." _
}
# ----- date helpers (BSD + GNU compatible) -----
today() { date +%Y-%m-%d; }
week_ago() { date -d '7 days ago' +%Y-%m-%d 2>/dev/null || date -v-7d +%Y-%m-%d; }
this_year(){ date +%Y; }
prompt_value() {
# prompt_value LABEL DEFAULT PLACEHOLDER -> stdout
local label="$1" default="$2" placeholder="$3"
gum input --prompt " $label > " --value "$default" --placeholder "$placeholder"
}
# ----- postgres reachability check -----
pg_ok() {
command -v pg_isready >/dev/null 2>&1 && \
pg_isready -h "${PGHOST:-localhost}" -p "${PGPORT:-5433}" -q 2>/dev/null
}
# ----- build menu -----
menu_build() {
header
section "Build"
local choice
choice=$(gum choose \
--header "What to build:" \
"nix build all executables" \
"cabal build all" \
"cabal build $PROJECT_NAME (library + pelotero)" \
"Back") || return
case "$choice" in
"nix build all executables")
header; section "Build all via nix"
if build_all; then ok "Done."; else err "One or more builds failed."; fi
pause ;;
"cabal build all")
header; section "cabal build all"
if cabal build all; then ok "Done."; else err "Build failed."; fi
pause ;;
"cabal build $PROJECT_NAME (library + pelotero)")
header; section "cabal build $PROJECT_NAME pelotero"
if cabal build "$PROJECT_NAME" pelotero; then ok "Done."; else err "Build failed."; fi
pause ;;
"Back") return ;;
esac
}
# ----- run app menu -----
menu_run_app() {
header
section "Run pelotero"
if ! pg_ok; then
info "Postgres not reachable; the CLI will fail at the migration step."
info "Use the Database menu to start it first."
echo ""
fi
local choice
choice=$(gum choose \
--header "Subcommand:" \
"db check" \
"sync rosters" \
"sync schedule" \
"sync boxscores" \
"snapshot lineups" \
"score" \
"draft run" \
"Back") || return
case "$choice" in
"db check")
header; section "pelotero db check"
cabal run pelotero -- db check
pause ;;
"sync rosters")
header; section "pelotero sync rosters"
local season
season=$(prompt_value "Season" "$(this_year)" "YYYY") || return
[ -z "$season" ] && return
cabal run pelotero -- sync rosters --season "$season"
pause ;;
"sync schedule")
header; section "pelotero sync schedule"
local from to
from=$(prompt_value "From" "$(week_ago)" "YYYY-MM-DD") || return
to=$(prompt_value "To" "$(today)" "YYYY-MM-DD") || return
{ [ -z "$from" ] || [ -z "$to" ]; } && return
cabal run pelotero -- sync schedule --from "$from" --to "$to"
pause ;;
"sync boxscores")
header; section "pelotero sync boxscores"
local from to
from=$(prompt_value "From" "$(week_ago)" "YYYY-MM-DD") || return
to=$(prompt_value "To" "$(today)" "YYYY-MM-DD") || return
{ [ -z "$from" ] || [ -z "$to" ]; } && return
cabal run pelotero -- sync boxscores --from "$from" --to "$to"
pause ;;
"snapshot lineups")
header; section "pelotero snapshot lineups"
local on
on=$(prompt_value "On date" "$(today)" "YYYY-MM-DD") || return
[ -z "$on" ] && return
cabal run pelotero -- snapshot lineups --on-date "$on"
pause ;;
"score")
header; section "pelotero score"
local lid
lid=$(prompt_value "League id" "1" "positive integer") || return
[ -z "$lid" ] && return
cabal run pelotero -- score --league-id "$lid"
pause ;;
"draft run")
header; section "pelotero draft run"
local lid
lid=$(prompt_value "League id" "1" "positive integer") || return
[ -z "$lid" ] && return
cabal run pelotero -- draft run --league-id "$lid"
pause ;;
"Back") return ;;
esac
}
# ----- test menu -----
menu_tests() {
header
section "Tests"
local choice
choice=$(gum choose \
--header "Test suite:" \
"Unit + property (pelotero-test)" \
"Integration (pelotero-integration-test, needs Postgres)" \
"All test suites" \
"Back") || return
case "$choice" in
"Unit + property (pelotero-test)")
header; section "cabal test pelotero-test"
cabal test pelotero-test
pause ;;
"Integration (pelotero-integration-test, needs Postgres)")
header; section "cabal test pelotero-integration-test"
if ! pg_ok; then
err "Postgres not reachable at ${PGHOST:-localhost}:${PGPORT:-5433}"
info "Start it from the Database menu first."
else
cabal test pelotero-integration-test
fi
pause ;;
"All test suites")
header; section "cabal test"
cabal test
pause ;;
"Back") return ;;
esac
}
# ----- database menu -----
menu_database() {
header
section "Database"
local choice
choice=$(gum choose \
--header "Action:" \
"Start postgres (pg-start)" \
"Stop postgres (pg-stop)" \
"Status (pg-stats)" \
"Connect (pg-connect)" \
"Backup (pg-backup)" \
"Restore from backup (pg-restore)" \
"Rotate credentials (pg-rotate-credentials)" \
"Apply migrations (pelotero db check)" \
"Back") || return
case "$choice" in
"Start postgres (pg-start)")
header; section "pg-start"
pg-start; pause ;;
"Stop postgres (pg-stop)")
header; section "pg-stop"
pg-stop; pause ;;
"Status (pg-stats)")
header; section "pg-stats"
pg-stats; pause ;;
"Connect (pg-connect)")
pg-connect ;;
"Backup (pg-backup)")
header; section "pg-backup"
pg-backup; pause ;;
"Restore from backup (pg-restore)")
header; section "pg-restore"
local backup_dir="$HOME/.local/share/$PROJECT_NAME/backups"
if [ ! -d "$backup_dir" ] || [ -z "$(ls -A "$backup_dir" 2>/dev/null)" ]; then
err "No backups in $backup_dir"
pause
return
fi
local choice_file
choice_file=$(ls -1t "$backup_dir" | gum choose --header "Backup file:") || return
[ -z "$choice_file" ] && return
pg-restore "$backup_dir/$choice_file"
pause ;;
"Rotate credentials (pg-rotate-credentials)")
header; section "pg-rotate-credentials"
pg-rotate-credentials; pause ;;
"Apply migrations (pelotero db check)")
header; section "pelotero db check"
cabal run pelotero -- db check
pause ;;
"Back") return ;;
esac
}
# ----- main loop -----
while true; do
header
if pg_ok; then
ok "Postgres reachable at ${PGHOST:-localhost}:${PGPORT:-5433}"
else
info "Postgres not reachable (Database -> Start postgres)"
fi
echo ""
ACTION=$(gum choose \
--cursor "> " \
--header " Choose:" \
"Build" \
"Run pelotero" \
"Tests" \
"Database" \
"Quit") || break
case "$ACTION" in
"Build") menu_build ;;
"Run pelotero") menu_run_app ;;
"Tests") menu_tests ;;
"Database") menu_database ;;
"Quit"|*) break ;;
esac
done
clear
gum style --foreground 2 --align center --width 64 --margin "1 2" \
"Goodbye from $PROJECT_NAME."