From a37e69ae5e96708e520dd4ca76075ae9f6f38fb0 Mon Sep 17 00:00:00 2001 From: Nick Hale <4175918+njhale@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:54:42 -0400 Subject: [PATCH] chore: refactor dev script and auto-open browser - Move dev.sh to the tools directory - Print colorful section headers to delimit dev script operations - Auto-open the admin page after otto8 server is ready Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com> --- Makefile | 3 +-- dev.sh | 23 ------------------ tools/dev.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 25 deletions(-) delete mode 100755 dev.sh create mode 100755 tools/dev.sh diff --git a/Makefile b/Makefile index 90afd2fcb..4c49a1598 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,7 @@ build: go build -o bin/otto8 -v dev: ui - @echo "Starting dev otto8 server and admin UI..." - ./dev.sh + ./tools/dev.sh # Lint the project lint: lint-admin diff --git a/dev.sh b/dev.sh deleted file mode 100755 index 775026552..000000000 --- a/dev.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -cleanup() { - echo "Terminating processes..." - kill 0 -} - -trap cleanup EXIT - -# Start the otto server -go run main.go server --dev-mode 2>&1 | while IFS= read -r line; do - printf "\033[38;5;183m[server]\033[0m %s\n" "$line" -done & -otto_pid=$! - -# Start the admin UI -cd ui/admin && VITE_API_IN_BROWSER=true npm run dev 2>&1 | while IFS= read -r line; do - printf "\033[38;5;153m[admin-ui]\033[0m %s\n" "$line" -done & -npm_pid=$! - -# Wait for both processes to finish -wait "$otto_pid" "$npm_pid" diff --git a/tools/dev.sh b/tools/dev.sh new file mode 100755 index 000000000..8025d8f6b --- /dev/null +++ b/tools/dev.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -e # Exit on any command failure + +print_section_header() { + local color_code=$1 + local message=$2 + local terminal_width=$(tput cols || echo 80) # Default to 80 if tput fails + local padding_width=$(( (terminal_width - ${#message} - 2) / 2 )) + local padding=$(printf '%*s' "$padding_width" '' | tr ' ' '-') + + echo -e "\033[38;5;${color_code}m${padding} ${message} ${padding}\033[0m" +} + +cleanup() { + kill 0 +} + +trap cleanup EXIT # Handles script exit and Ctrl-C + +print_section_header 120 "Starting otto8 server and admin UI..." + +# Start the otto server +( + go run main.go server --dev-mode 2>&1 | while IFS= read -r line; do + printf "\033[38;5;183m[server]\033[0m %s\n" "$line" + done +) & +otto_pid=$! + +# Start the admin UI +( + cd ui/admin + VITE_API_IN_BROWSER=true npm run dev 2>&1 | while IFS= read -r line; do + printf "\033[38;5;153m[admin-ui]\033[0m %s\n" "$line" + done +) & +npm_pid=$! + +# Health check and open the browser +( + source .envrc.dev + healthcheck_passed=false + for _ in {1..60}; do # ~1 minute timeout + if kubectl get --raw /healthz &>/dev/null; then + healthcheck_passed=true + break + fi + sleep 1 + done + + if [[ "$healthcheck_passed" != true ]]; then + print_section_header 196 "Timeout waiting for otto8 server to be ready" + cleanup + fi + + if command -v open >/dev/null; then + open http://localhost:8080/admin/ + elif command -v xdg-open >/dev/null; then + xdg-open http://localhost:8080/admin/ + else + echo "Open http://localhost:8080/admin/ in your browser." + fi + print_section_header 120 "otto8 server and admin UI ready" +) & +healthcheck_pid=$! + +# Wait for either otto or npm process to exit, then trigger cleanup +wait "$healthcheck_pid" "$otto_pid" "$npm_pid"