-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-store.sh
More file actions
executable file
Β·54 lines (44 loc) Β· 2.42 KB
/
setup-store.sh
File metadata and controls
executable file
Β·54 lines (44 loc) Β· 2.42 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
#!/bin/bash
# setup-store.sh
# Initializes or verifies that a Cloudflare Secrets Store exists
# Creates a store named "main-credentials-store" if none exists
set -e
STORE_NAME="main-credentials-store"
echo "π Checking for existing Secrets Store..."
# List stores and check if any exist
STORES_OUTPUT=$(npx wrangler secrets-store store list --remote 2>&1 || true)
# Check if stores exist by looking for store names in the output
STORE_LINE=$(echo "$STORES_OUTPUT" | grep -E "default_secrets_store|main-credentials-store" | head -n 1)
if [ -z "$STORE_LINE" ]; then
# Try alternative parsing - look for table data rows
STORE_LINE=$(echo "$STORES_OUTPUT" | grep -E "\β.*\β.*\β" | grep -v "Name\|βββ\|AccountID" | head -n 1)
fi
if [ -n "$STORE_LINE" ]; then
# Store exists
STORE_NAME_FOUND=$(echo "$STORE_LINE" | awk -F'β' '{print $2}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
STORE_ID=$(echo "$STORE_LINE" | awk -F'β' '{print $3}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "β
Store found: $STORE_NAME_FOUND (ID: $STORE_ID)"
echo " Store ID saved for use in other scripts"
else
# No store found, try to create one
echo "π¦ No store found. Creating new store: $STORE_NAME"
CREATE_OUTPUT=$(npx wrangler secrets-store store create "$STORE_NAME" --remote 2>&1 || true)
# Check if creation was successful or if limit was reached
if echo "$CREATE_OUTPUT" | grep -q "maximum_stores_exceeded"; then
echo "β οΈ Maximum stores limit reached. Checking for existing store..."
# Try to get the existing store
STORES_OUTPUT=$(npx wrangler secrets-store store list --remote 2>&1 || true)
STORE_LINE=$(echo "$STORES_OUTPUT" | grep -E "\β.*\β.*\β" | grep -v "Name\|βββ\|AccountID" | head -n 1)
if [ -n "$STORE_LINE" ]; then
STORE_NAME_FOUND=$(echo "$STORE_LINE" | awk -F'β' '{print $2}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
STORE_ID=$(echo "$STORE_LINE" | awk -F'β' '{print $3}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "β
Using existing store: $STORE_NAME_FOUND (ID: $STORE_ID)"
fi
elif echo "$CREATE_OUTPUT" | grep -q "Created\|created"; then
echo "β
Store created successfully!"
else
echo "β οΈ Could not create store. Check output above for details."
fi
fi
echo ""
echo "π‘ Tip: Use './get-store-id.sh' to retrieve the store ID anytime"