#!/bin/sh

set -e

KV_TITLE=$1

if [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then
  echo "CLOUDFLARE_ACCOUNT_ID is missing"
  exit 1;
fi;

if [ -z "$CLOUDFLARE_TOKEN" ]; then
  echo "CLOUDFLARE_TOKEN is missing"
  exit 1;
fi;

curl -s https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces \
  -H "authorization: Bearer $CLOUDFLARE_TOKEN" \
  | jq -r ".result | map(select(.title == \"$KV_TITLE\")) | .[0].id | select (.!=null)"
