#!/bin/sh
# Lists the flag types and expected lifetimes built into the open-source Unleash server.
# Usage: sh benchmarks/flag-types/unleash-types.sh > benchmarks/flag-types/unleash-types.txt
set -e
IMAGE=unleashorg/unleash-server:8.3.1
cleanup() { docker rm -f -v ft-unleash ft-db >/dev/null 2>&1 || true; docker network rm ft-net >/dev/null 2>&1 || true; }
trap cleanup EXIT
cleanup
docker network create ft-net >/dev/null
docker run -d --name ft-db --network ft-net -e POSTGRES_DB=db -e POSTGRES_HOST_AUTH_METHOD=trust postgres:15 >/dev/null
until docker exec ft-db pg_isready -U postgres >/dev/null 2>&1; do sleep 1; done
docker run -d --name ft-unleash --network ft-net -p 4243:4242 -e DATABASE_URL=postgres://postgres@ft-db/db -e DATABASE_SSL=false \
  -e LOG_LEVEL=warn -e "INIT_ADMIN_API_TOKENS=*:*.unleash-insecure-admin-api-token" "$IMAGE" >/dev/null
until curl -sf localhost:4243/health >/dev/null; do sleep 1; done
echo "# $IMAGE, GET /api/admin/feature-types, $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "id | name | lifetimeDays | description"
curl -s localhost:4243/api/admin/feature-types -H 'authorization: *:*.unleash-insecure-admin-api-token' |
  python3 -c "import json,sys; [print(t['id'], '|', t['name'], '|', t.get('lifetimeDays'), '|', t['description']) for t in json.load(sys.stdin)['types']]"
