[tools]
minikube = "1.37.0"
pack = "0.39.0"
"ubi:railwayapp/railpack" = "0.15.1"
"cargo:sqlx-cli" = "latest"
"cargo:cargo-all-features" = "latest"
mdbook = "latest"
helm = "latest"
rust = { version = "1.91.1", profile = "default" }

[tasks."sqlx:prepare"]
description = "Prepare the SQLX query cache that needs to be committed for building with SQLX_OFFLINE=true."
run = "cargo sqlx prepare -- --features server,aws,k8s,snowflake --all-targets"

[tasks."sqlx:check"]
description = "Check that all SQLX queries are valid."
run = "cargo sqlx prepare --check -- --features server,aws,k8s,snowflake --all-targets"

[tasks.lint]
description = "Run all linting checks (clippy, formatting, sqlx, helm)."
run = """
SQLX_OFFLINE=true cargo all-features check --all-targets
SQLX_OFFLINE=true cargo all-features clippy --all-targets -- -D warnings
cargo fmt --all -- --check
mise sqlx:check
helm lint helm/rise
"""

[tasks."docs:serve"]
description = "Serve documentation with live reload in port 3001."
run = "mdbook serve -p 3001 --open"

[tasks."db:nuke"]
description = "Drop and recreate the development database. USE WITH CAUTION."
run = """
set -x
docker compose down -v postgres
docker compose up postgres -d
"""

[tasks."db:migrate"]
description = "Run database migrations. Run before building the backend."
run = "docker compose up postgres -d && sleep 2 && sqlx migrate run"

[tasks."backend:deps"]
description = "Start all backend dependencies with docker compose."
run = "docker compose up -d"

[tasks."backend:run"]
description = "Run the backend server."
depends = ["backend:deps", "db:migrate"]
run = "cargo run --features server,k8s,aws,snowflake -- backend server"
alias = "br"

[tasks."minikube:launch"]
description = "Start minikube, preconfigured with access to the local Docker registry (rise-registry)."
run = """
set -x

docker compose up -d registry
NETWORK_NAME=rise_default
REGISTRY_NAME=rise-registry
REGISTRY_DIR="/etc/containerd/certs.d/localhost:5000"

minikube delete
minikube start --driver=docker --insecure-registry="${REGISTRY_NAME}:5000" --memory=4096mb --cpus=2
minikube addons enable ingress

for node in $(minikube node list | awk '{print $1}'); do
  docker exec "${node}" mkdir -p "${REGISTRY_DIR}"
  cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml"
[host."http://${REGISTRY_NAME}:5000"]
EOF
done

docker network connect "${NETWORK_NAME}" minikube || true

echo
echo "Minikube started. Port-forawrding ingress-nginx-controller to localhost:8080 (HTTP) and localhost:8443 (HTTPS)..."
echo "You might want to update your /etc/hosts to point your local domains to 127.0.0.1"
echo

kubectl port-forward --namespace ingress-nginx svc/ingress-nginx-controller 8080:80 8443:443
"""
