-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdev
More file actions
executable file
·745 lines (657 loc) · 28.1 KB
/
dev
File metadata and controls
executable file
·745 lines (657 loc) · 28.1 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#!/usr/bin/env bash
# Dev command wrapper for docker compose management
# Provides convenient shortcuts for common development operations
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
COMPOSE_FILE="compose.yml"
SERVICE_NAME="app"
# Export current user's UID/GID for Docker to use
# This ensures files created in containers are owned by the host user
export LOCAL_UID=$(id -u)
export LOCAL_GID=$(id -g)
# Check if docker is available
if ! command -v docker &> /dev/null; then
echo -e "${RED}Error: docker is not installed or not in PATH${NC}"
echo "Please install Docker: https://docs.docker.com/get-docker/"
exit 1
fi
# Check if docker compose is available
if ! docker compose version &> /dev/null; then
echo -e "${RED}Error: docker compose is not available${NC}"
echo "Please install Docker Compose: https://docs.docker.com/compose/install/"
exit 1
fi
# Check if compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
echo -e "${RED}Error: $COMPOSE_FILE not found${NC}"
echo "Please run this script from the project root directory"
exit 1
fi
# Show help if no arguments provided
if [ $# -eq 0 ]; then
cat <<EOF
${GREEN}Mydia Development Helper${NC}
Usage: ./dev <command> [arguments...]
${YELLOW}Common Commands:${NC}
up [-d] Start services (add -d for detached mode)
down Stop and remove containers
restart Restart services
logs [-f] [service] View logs (add -f to follow)
ps List running containers
stop Stop services
start Start services
${YELLOW}Container Commands:${NC}
shell Open an interactive shell in the app container
mix <args> Run mix commands inside the container
iex Start IEx inside the container
bash Open bash shell in the app container
${YELLOW}Development Commands:${NC}
test [args] Run tests (mix test)
feature-test [args] Run Wallaby browser tests
format Format code (mix format)
deps.get Install dependencies
ecto.migrate Run database migrations
ecto.reset Reset database
phx.server Start Phoenix server
mix precommit Run precommit checks in isolated container
${YELLOW}User Management:${NC}
user list [--role=R] List all users (optionally filter by role)
user add <email> <username> [opts]
Add a new user (--password, --role, --display-name)
user delete <id> Delete a user by email or username
user reset-password <id> [--password=P]
Reset a user's password
${YELLOW}Player (Flutter in App Container):${NC}
flutter <args> Run any flutter command in app container
player setup Install deps and run code generation
player build Build web assets (FlutterWatcher handles deployment)
player shell Open shell in player directory
player logs Follow build_runner logs
player restart Restart app container (includes build_runner)
player e2e [opts] Run player E2E integration tests (native Linux app)
player e2e-build Pre-build E2E images (faster subsequent runs)
${YELLOW}Player Android (Native, uses player/flake.nix):${NC}
player android build Build Android APK (release)
player android run Build and run on connected Android device
player android shell Open nix develop shell in player directory
${YELLOW}Player E2E Options:${NC}
--build Rebuild Docker images before running tests
--target <path> Test file to run (default: pairing_flow_test.dart)
-- <flutter-args> Extra args passed to flutter test
${YELLOW}Player Development:${NC}
Access: http://localhost:4000/player
- FlutterWatcher auto-builds on file changes
- build_runner watch handles code generation
${YELLOW}Metadata Relay:${NC}
relay deploy Build, push :dev image, and redeploy to k8s
relay build Build :dev Docker image only
relay push Push :dev image to ghcr.io
relay restart Restart deployment (pull latest image)
${YELLOW}Docker Compose Commands:${NC}
exec <service> <cmd> Execute command in running service
run <service> <cmd> Run one-off command in new container
build [--no-cache] Build or rebuild services
pull Pull service images
${YELLOW}Examples:${NC}
./dev up -d # Start services in background
./dev logs -f # Follow logs
./dev shell # Open interactive shell
./dev mix test # Run tests
./dev feature-test # Run Wallaby browser tests
./dev mix ecto.migrate # Run migrations
./dev exec app mix phx.routes # Show Phoenix routes
./dev user list # List all users
./dev user add me@example.com myuser --password=secret123 --role=admin
./dev user reset-password admin --password=newpassword
./dev player logs # Follow build_runner logs
./dev player build # Build Flutter web assets
Any other command will be forwarded directly to docker compose.
EOF
exit 0
fi
# Check if the app container is running
is_container_running() {
docker compose ps --status=running --format '{{.Service}}' 2>/dev/null | grep -q "^${SERVICE_NAME}$"
}
# Helper function to run commands - uses exec if container is running, otherwise run
run_in_container() {
if is_container_running; then
# Use exec for faster execution when container is already running
# Pass --user to run as the same UID/GID as the host user
docker compose exec --user "$LOCAL_UID:$LOCAL_GID" "$SERVICE_NAME" "$@"
else
# Use run for ephemeral containers when nothing is running
# The entrypoint will handle user switching via LOCAL_UID/LOCAL_GID env vars
docker compose run --rm "$SERVICE_NAME" "$@"
fi
}
# Get the app service image name
# Returns the image name from compose config, or builds it if not available
get_app_image() {
local image_name
# Get image name from compose config (now explicitly defined as mydia-app:latest)
image_name=$(docker compose config --format json 2>/dev/null | jq -r '.services.app.image // empty')
if [ -z "$image_name" ]; then
# Fallback for older docker compose versions
image_name="mydia-app:latest"
fi
# Check if image exists locally
if ! docker image inspect "$image_name" &>/dev/null; then
echo -e "${YELLOW}App image not found. Building...${NC}" >&2
docker compose build app >&2
fi
echo "$image_name"
}
# Run command in an isolated container (separate volumes from dev)
# Usage: run_isolated <command>
# Example: run_isolated "mix test"
run_isolated() {
local cmd="$1"
local app_image
app_image=$(get_app_image)
# Use dedicated isolated volumes (separate from dev, but cached between runs)
# Note: Source must be read-write because Mix touches .ex files during compilation
docker run --rm \
-e MIX_ENV=test \
-e DATABASE_PATH=/tmp/mydia_test.db \
-e LOCAL_UID="$LOCAL_UID" \
-e LOCAL_GID="$LOCAL_GID" \
-e SECRET_KEY_BASE="test_secret_key_base_at_least_64_bytes_long_for_isolated_container" \
-e MIX_HOME=/app/.mix \
-e HEX_HOME=/app/.hex \
-v "$(pwd):/app" \
-v mydia_isolated_build:/app/_build \
-v mydia_isolated_deps:/app/deps \
-v mydia_isolated_mix:/app/.mix \
-v mydia_isolated_hex:/app/.hex \
-w /app \
--entrypoint "" \
"$app_image" \
sh -c "(mix deps.check --all > /dev/null 2>&1 || mix deps.get --quiet) && $cmd"
}
# Run a single precommit step with compact output
# Usage: run_step <label> <command>
# Returns 0 on success, 1 on failure (stores output for failure dump)
run_step() {
local label="$1"
local cmd="$2"
local output
local exit_code
printf " %-30s" "$label"
local start_time=$SECONDS
output=$(eval "$cmd" 2>&1)
exit_code=$?
local elapsed=$(( SECONDS - start_time ))
if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}✓${NC} ${elapsed}s"
else
echo -e "${RED}✗${NC} ${elapsed}s"
FAILED_STEPS+=("$label")
FAILED_OUTPUTS+=("$output")
fi
return $exit_code
}
# Run a single precommit step for tests (extracts summary on success)
# Usage: run_step_test <label> <command>
run_step_test() {
local label="$1"
local cmd="$2"
local output
local exit_code
printf " %-30s" "$label"
local start_time=$SECONDS
output=$(eval "$cmd" 2>&1)
exit_code=$?
local elapsed=$(( SECONDS - start_time ))
if [ $exit_code -eq 0 ]; then
# Extract test summary line (e.g., "186 tests, 0 failures")
local summary
summary=$(echo "$output" | grep -oE '[0-9]+ tests?, [0-9]+ failures?' | tail -1)
if [ -n "$summary" ]; then
echo -e "${GREEN}✓${NC} $summary (${elapsed}s)"
else
echo -e "${GREEN}✓${NC} ${elapsed}s"
fi
else
echo -e "${RED}✗${NC} ${elapsed}s"
# Save full output to temp file for inspection
local full_output_file
full_output_file=$(mktemp /tmp/mydia-test-output-XXXXXX.log)
echo "$output" > "$full_output_file"
# Extract only failure blocks and summary, stripping noise
local filtered
filtered=$(echo "$output" | sed 's/\x1b\[[0-9;]*m//g' | sed -n '/^ [0-9]\+) /,/^$/p; /^Finished in/p; /^[0-9]\+ tests\?/p')
FAILED_STEPS+=("$label")
FAILED_OUTPUTS+=("$(printf '%s\n\nFull output: %s' "$filtered" "$full_output_file")")
fi
return $exit_code
}
# Run precommit checks with an ephemeral PostgreSQL container for parallel tests
run_precommit() {
local app_image
app_image=$(get_app_image)
# Clean up any stale precommit containers/networks from previous failed runs
docker ps -a --filter "name=mydia-precommit-pg-" --format "{{.ID}}" | xargs -r docker rm -f &>/dev/null || true
docker network ls --filter "name=mydia-precommit-" --format "{{.ID}}" | xargs -r docker network rm &>/dev/null || true
local network_name="mydia-precommit-$$"
local pg_container="mydia-precommit-pg-$$"
# Cleanup function - runs on exit, interrupt, and termination
cleanup_precommit() {
docker rm -f "$pg_container" &>/dev/null || true
docker network rm "$network_name" &>/dev/null || true
}
trap cleanup_precommit EXIT INT TERM
# Create temporary Docker network
docker network create "$network_name" &>/dev/null
# Start ephemeral PostgreSQL container
docker run -d --rm \
--name "$pg_container" \
--network "$network_name" \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=mydia_test \
postgres:16-alpine \
&>/dev/null
# Wait for PostgreSQL to be ready
local retries=30
while [ $retries -gt 0 ]; do
if docker exec "$pg_container" pg_isready -U postgres &>/dev/null; then
break
fi
retries=$((retries - 1))
sleep 0.3
done
if [ $retries -eq 0 ]; then
echo -e "${RED}Error: PostgreSQL failed to start${NC}"
return 1
fi
echo "Precommit"
# Track failures
FAILED_STEPS=()
FAILED_OUTPUTS=()
local any_failed=0
# Helper to run mix commands inside the container
# Uses separate build volume from run_isolated to avoid compile-time config conflicts
# (run_isolated compiles for SQLite, precommit compiles for PostgreSQL)
run_mix() {
docker run --rm \
--network "$network_name" \
-e MIX_ENV=test \
-e DATABASE_TYPE=postgres \
-e DATABASE_HOST="$pg_container" \
-e DATABASE_PORT=5432 \
-e DATABASE_USER=postgres \
-e DATABASE_PASSWORD=postgres \
-e DATABASE_NAME=mydia_test \
-e LOCAL_UID="$LOCAL_UID" \
-e LOCAL_GID="$LOCAL_GID" \
-e SECRET_KEY_BASE="test_secret_key_base_at_least_64_bytes_long_for_isolated_container" \
-e MIX_HOME=/app/.mix \
-e HEX_HOME=/app/.hex \
-v "$(pwd):/app" \
-v mydia_precommit_build:/app/_build \
-v mydia_isolated_deps:/app/deps \
-v mydia_isolated_mix:/app/.mix \
-v mydia_isolated_hex:/app/.hex \
-w /app \
--entrypoint "" \
"$app_image" \
sh -c "$1"
}
# Step 1: Dependencies
run_step "Dependencies" "run_mix 'mix deps.check --all > /dev/null 2>&1 || mix deps.get --quiet'" || any_failed=1
# Step 2: Compile (warnings-as-errors enforced by mix.exs)
run_step "Compile" "run_mix 'mix compile'" || any_failed=1
# Step 3: Unused deps
run_step "Unused deps" "run_mix 'mix deps.unlock --unused'" || any_failed=1
# Step 4: Format check
run_step "Format" "run_mix 'mix format --check-formatted'" || any_failed=1
# Step 5: Database setup
run_step "Database" "run_mix 'mix ecto.create --quiet && mix ecto.migrate --quiet'" || any_failed=1
# Step 6: Tests
run_step_test "Tests" "run_mix 'mix test --color'" || any_failed=1
echo ""
# Print failure details
if [ ${#FAILED_OUTPUTS[@]} -gt 0 ]; then
for i in "${!FAILED_STEPS[@]}"; do
echo -e "${RED}── ${FAILED_STEPS[$i]} ──${NC}"
echo "${FAILED_OUTPUTS[$i]}"
echo ""
done
fi
if [ $any_failed -ne 0 ]; then
echo -e "${RED}✗ Precommit failed${NC}"
return 1
else
echo -e "${GREEN}✓ Precommit passed${NC}"
return 0
fi
}
# Parse command
COMMAND=$1
shift
case "$COMMAND" in
# Interactive shell shortcuts
shell|sh)
echo -e "${GREEN}Opening interactive shell in $SERVICE_NAME container...${NC}"
run_in_container sh
;;
bash)
echo -e "${GREEN}Opening bash shell in $SERVICE_NAME container...${NC}"
run_in_container bash
;;
iex)
echo -e "${GREEN}Starting IEx in $SERVICE_NAME container...${NC}"
# If you pass flags (e.g. -e), forward them to iex.
# Default is interactive iex -S mix.
if [ $# -gt 0 ]; then
run_in_container iex "$@"
else
run_in_container iex -S mix
fi
;;
# Mix command shortcuts
mix)
# Run precommit/test in a fully isolated container (no shared volumes)
if [[ "$1" == "precommit" ]]; then
echo -e "${GREEN}Running precommit in isolated container...${NC}"
run_precommit
elif [[ "$1" == "test" ]]; then
echo -e "${GREEN}Running tests in isolated container...${NC}"
FAILED_STEPS=()
FAILED_OUTPUTS=()
run_step_test "Tests" "run_isolated 'mix ecto.create --quiet && mix ecto.migrate --quiet && mix test --color ${*:2}'"
if [ ${#FAILED_OUTPUTS[@]} -gt 0 ]; then
echo ""
echo "${FAILED_OUTPUTS[0]}"
fi
else
run_in_container mix "$@"
fi
;;
# Common development shortcuts
test)
echo -e "${GREEN}Running tests in isolated container...${NC}"
FAILED_STEPS=()
FAILED_OUTPUTS=()
run_step_test "Tests" "run_isolated 'mix ecto.create --quiet && mix ecto.migrate --quiet && mix test --color $*'"
if [ ${#FAILED_OUTPUTS[@]} -gt 0 ]; then
echo ""
echo "${FAILED_OUTPUTS[0]}"
fi
;;
feature-test)
echo -e "${GREEN}Running feature tests (Wallaby browser tests)...${NC}"
docker compose run --rm -e MIX_ENV=test -e LOCAL_UID="$LOCAL_UID" -e LOCAL_GID="$LOCAL_GID" "$SERVICE_NAME" ./scripts/run-feature-tests.sh "$@"
;;
format)
echo -e "${GREEN}Formatting code...${NC}"
run_in_container mix format
;;
deps.get)
echo -e "${GREEN}Installing dependencies...${NC}"
run_in_container mix deps.get
;;
ecto.migrate)
echo -e "${GREEN}Running migrations...${NC}"
run_in_container mix ecto.migrate
;;
ecto.reset)
echo -e "${GREEN}Resetting database...${NC}"
run_in_container mix ecto.reset
;;
phx.server)
echo -e "${GREEN}Starting Phoenix server...${NC}"
run_in_container mix phx.server
;;
# User management
user)
if [ $# -eq 0 ]; then
echo -e "${RED}Error: user command requires a subcommand${NC}"
echo "Usage: ./dev user <list|add|delete|reset-password> [args...]"
exit 1
fi
run_in_container mix mydia.user "$@"
;;
# Flutter commands (runs in app container)
flutter)
echo -e "${GREEN}Running flutter command in app container...${NC}"
run_in_container sh -c "cd player && flutter $*"
;;
player)
case "$1" in
logs)
echo -e "${GREEN}Following build_runner logs from app container...${NC}"
docker compose exec "$SERVICE_NAME" tail -f /tmp/build_runner.log
;;
build)
echo -e "${GREEN}Building player web assets...${NC}"
run_in_container sh -c "cd player && flutter build web --release --base-href /player/ --tree-shake-icons"
echo -e "${GREEN}Player web assets built successfully!${NC}"
echo -e "${GREEN}Copying build output to priv/static/player...${NC}"
run_in_container sh -c "rm -rf priv/static/player && cp -r player/build/web priv/static/player"
# Copy manifest.json if it exists (Flutter doesn't auto-copy it)
run_in_container sh -c "[ -f player/web/manifest.json ] && cp player/web/manifest.json priv/static/player/"
echo -e "${GREEN}Build deployed successfully!${NC}"
;;
setup)
echo -e "${GREEN}Setting up player project (pub get + build_runner)...${NC}"
run_in_container sh -c "cd player && flutter pub get"
run_in_container sh -c "cd player && flutter pub run build_runner build --delete-conflicting-outputs"
echo -e "${GREEN}Player project setup complete!${NC}"
;;
shell)
echo -e "${GREEN}Opening shell in player directory (app container)...${NC}"
run_in_container sh -c "cd player && exec bash"
;;
restart)
echo -e "${GREEN}Restarting app container (includes build_runner)...${NC}"
docker compose restart "$SERVICE_NAME"
echo -e "${GREEN}App container restarted. Follow build_runner logs with: ./dev player logs${NC}"
;;
e2e)
shift
rebuild_images=false
test_target="integration_test/pairing_flow_test.dart"
test_args=""
while [ $# -gt 0 ]; do
case "$1" in
--build)
rebuild_images=true
;;
--target)
if [ -z "$2" ]; then
echo -e "${RED}Error: --target requires a test file path${NC}"
echo "Example: ./dev player e2e --target integration_test/p2p_streaming_test.dart"
exit 1
fi
test_target="$2"
shift
;;
--)
shift
if [ $# -gt 0 ]; then
test_args="$*"
fi
break
;;
*)
test_args="${test_args:+$test_args }$1"
;;
esac
shift
done
echo -e "${GREEN}Running player E2E integration tests (native Linux app)...${NC}"
echo ""
echo "This will:"
echo " 1. Build and start metadata-relay (claim code generation path)"
echo " 2. Build and start Mydia (E2E mode with test admin user)"
echo " 3. Build Linux test runner image (Flutter desktop + Rust/iroh)"
echo " 4. Run Flutter integration tests on Linux (xvfb, not web/chrome)"
echo ""
echo "Test target: $test_target"
if [ -n "$test_args" ]; then
echo "Extra test args: $test_args"
fi
echo ""
echo "Test flow: claim code → Noise handshake → iroh P2P pairing/device registration"
echo ""
if [ "$rebuild_images" = true ]; then
echo "Image rebuild: enabled (--build)"
else
echo "Image rebuild: disabled (reuses cached images)"
echo "Tip: add '--build' only when Dockerfiles/deps changed"
fi
echo ""
if [ "$rebuild_images" = true ]; then
DOCKER_BUILDKIT=1 docker compose -f compose.player-e2e.yml build
fi
E2E_TEST_TARGET="$test_target" E2E_TEST_ARGS="$test_args" \
docker compose -f compose.player-e2e.yml up --abort-on-container-exit --exit-code-from test
exit_code=$?
# Clean up
docker compose -f compose.player-e2e.yml down -v
exit $exit_code
;;
e2e-build)
echo -e "${GREEN}Pre-building player E2E images (without running tests)...${NC}"
echo ""
echo "This pre-builds all E2E Docker images to cache dependencies."
echo "Subsequent './dev player e2e' runs will be faster."
echo ""
DOCKER_BUILDKIT=1 docker compose -f compose.player-e2e.yml build
echo ""
echo -e "${GREEN}E2E images pre-built successfully!${NC}"
echo "Now run './dev player e2e' to execute the tests."
;;
android)
# Android builds use the player's flake.nix (native, not in Docker)
if ! command -v nix &> /dev/null; then
echo -e "${RED}Error: nix is not installed${NC}"
echo "Android builds require Nix. Install from: https://nixos.org/download.html"
exit 1
fi
case "$2" in
build)
echo -e "${GREEN}Building Android APK using player/flake.nix...${NC}"
echo ""
nix develop ./player --command bash -c "cd player && flutter pub get && flutter build apk --release"
if [ $? -eq 0 ]; then
echo ""
echo -e "${GREEN}APK built successfully!${NC}"
echo "Output: player/build/app/outputs/flutter-apk/app-release.apk"
fi
;;
run)
echo -e "${GREEN}Building and running on Android device...${NC}"
echo ""
extra_args="${@:3}"
nix develop ./player --command bash -c "cd player && flutter pub get && flutter run $extra_args"
;;
shell)
echo -e "${GREEN}Opening nix develop shell in player directory...${NC}"
echo ""
cd player && nix develop
;;
*)
echo -e "${RED}Error: Unknown android command: $2${NC}"
echo "Usage: ./dev player android <build|run|shell>"
exit 1
;;
esac
;;
*)
echo -e "${RED}Error: Unknown player command: $1${NC}"
echo "Usage: ./dev player <logs|build|setup|shell|restart|e2e|e2e-build|android>"
exit 1
;;
esac
;;
# Metadata relay deployment
relay)
RELAY_IMAGE="ghcr.io/getmydia/mydia/metadata-relay:dev"
RELAY_NAMESPACE="metadata-relay"
RELAY_DEPLOYMENT="metadata-relay"
case "$1" in
build)
echo -e "${GREEN}Building metadata-relay:dev Docker image...${NC}"
docker build -f metadata-relay/Dockerfile -t "$RELAY_IMAGE" .
echo -e "${GREEN}Build complete!${NC}"
;;
push)
echo -e "${GREEN}Pushing metadata-relay:dev to ghcr.io...${NC}"
docker push "$RELAY_IMAGE"
echo -e "${GREEN}Push complete!${NC}"
;;
restart)
echo -e "${GREEN}Restarting metadata-relay deployment...${NC}"
kubectl rollout restart deployment/"$RELAY_DEPLOYMENT" -n "$RELAY_NAMESPACE"
echo -e "${YELLOW}Waiting for rollout to complete...${NC}"
kubectl rollout status deployment/"$RELAY_DEPLOYMENT" -n "$RELAY_NAMESPACE" --timeout=120s
echo -e "${GREEN}Deployment restarted successfully!${NC}"
;;
deploy)
echo -e "${GREEN}Building, pushing, and deploying metadata-relay:dev...${NC}"
echo ""
# Build
echo -e "${YELLOW}[1/3] Building Docker image...${NC}"
docker build -f metadata-relay/Dockerfile -t "$RELAY_IMAGE" .
if [ $? -ne 0 ]; then
echo -e "${RED}Build failed!${NC}"
exit 1
fi
echo -e "${GREEN}Build complete!${NC}"
echo ""
# Push
echo -e "${YELLOW}[2/3] Pushing to ghcr.io...${NC}"
docker push "$RELAY_IMAGE"
if [ $? -ne 0 ]; then
echo -e "${RED}Push failed!${NC}"
exit 1
fi
PUSHED_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$RELAY_IMAGE" 2>/dev/null | cut -d'@' -f2)
echo -e "${GREEN}Push complete!${NC}"
echo ""
# Deploy
echo -e "${YELLOW}[3/3] Restarting deployment...${NC}"
kubectl rollout restart deployment/"$RELAY_DEPLOYMENT" -n "$RELAY_NAMESPACE"
kubectl rollout status deployment/"$RELAY_DEPLOYMENT" -n "$RELAY_NAMESPACE" --timeout=120s
if [ $? -ne 0 ]; then
echo -e "${RED}Deployment failed!${NC}"
exit 1
fi
# Verify digest
echo ""
echo -e "${YELLOW}Verifying deployment...${NC}"
RUNNING_DIGEST=$(kubectl get pod -n "$RELAY_NAMESPACE" -l app.kubernetes.io/name=metadata-relay -o jsonpath='{.items[0].status.containerStatuses[0].imageID}' 2>/dev/null | cut -d'@' -f2)
if [ -n "$PUSHED_DIGEST" ] && [ -n "$RUNNING_DIGEST" ]; then
if [ "$PUSHED_DIGEST" = "$RUNNING_DIGEST" ]; then
echo -e "${GREEN}Image digests match!${NC}"
else
echo -e "${YELLOW}Warning: Digests don't match (may need time to pull)${NC}"
echo " Pushed: $PUSHED_DIGEST"
echo " Running: $RUNNING_DIGEST"
fi
fi
echo ""
echo -e "${GREEN}Deployment complete!${NC}"
echo " Image: $RELAY_IMAGE"
echo " Digest: $PUSHED_DIGEST"
;;
*)
echo -e "${RED}Error: Unknown relay command: $1${NC}"
echo "Usage: ./dev relay <deploy|build|push|restart>"
exit 1
;;
esac
;;
# Forward everything else to docker compose
*)
docker compose "$COMMAND" "$@"
;;
esac