Skip to content

Instantly share code, notes, and snippets.

@simenandre
Created September 11, 2024 10:44
Show Gist options
  • Save simenandre/bbc68d5dda9d4ff8c84a5001d4eb563f to your computer and use it in GitHub Desktop.
Save simenandre/bbc68d5dda9d4ff8c84a5001d4eb563f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if environment variables are set
if [ -z "$TURSO_GROUP" ]; then
echo "Error: TURSO_GROUP environment variable is not set."
exit 1
fi
if [ -z "$TURSO_BASE_DB" ]; then
echo "Error: TURSO_BASE_DB environment variable is not set."
exit 1
fi
# Check if .env file exists
if [ ! -f .env ]; then
echo "Error: .env file not found. Please run 'task generate:env' to create the .env file."
exit 1
fi
# Check if turso is installed
if ! command -v turso &>/dev/null; then
echo "Error: turso command is not installed. Please install it and try again."
exit 1
fi
# Function to update .env file
update_env() {
if grep -q "TURSO_DATABASE_URL" .env; then
# Create a temporary file
temp_file=$(mktemp)
# Use sed to replace the line and output to the temporary file
sed 's|TURSO_DATABASE_URL=libsql://.*-padeia.turso.io|TURSO_DATABASE_URL=libsql://'$1'-padeia.turso.io|' .env >"$temp_file"
# Check if sed operation was successful
if [ $? -eq 0 ]; then
# If successful, move the temporary file to replace the original .env
mv "$temp_file" .env
echo "Updated TURSO_DATABASE_URL to use $1"
else
echo "Error: Failed to update .env file."
rm "$temp_file"
exit 1
fi
else
echo "Error: TURSO_DATABASE_URL not found in .env file."
echo "Please run 'task generate:env' to create the .env file with the necessary variables."
exit 1
fi
}
# Function to create a new database
create_db() {
local db_name="$1"
local creation_type="$2"
echo "Creating new database: $db_name"
if [ "$creation_type" = "blank" ]; then
turso db create --group "$TURSO_GROUP" "$db_name"
else
turso db create --group "$TURSO_GROUP" --from-db "$TURSO_BASE_DB" "$db_name"
fi
if [ $? -eq 0 ]; then
echo "Database $db_name created successfully."
update_env "$db_name"
else
echo "Failed to create database $db_name."
fi
}
# Fetch databases
DATABASES=$(turso db list -g "$TURSO_GROUP" | tail -n +2 | awk '{print $1}')
selectWithDefault() {
local item i=0 numItems=$# defaultIndex=0
# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$@"; do
[[ "$item" == !* ]] && defaultIndex=$(($i + 1)) && item="${item:1} [default]"
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.
# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && index=$defaultIndex && break # empty input == default choice
((index >= 1 && index <= numItems)) 2>/dev/null || {
echo "Invalid selection. Please try again." >&2
continue
}
break
done
# Output the selected *index* (1-based).
printf $index
}
# Function to prompt for new database creation
prompt_new_db() {
read -p "Enter name for new database: " NEW_DB_NAME
echo "Select database creation type (press Enter for default):"
type=$(
selectWithDefault "!Based on $TURSO_BASE_DB" "Blank database"
)
case $type in
1)
create_db "$NEW_DB_NAME" "from-prod"
;;
2)
create_db "$NEW_DB_NAME" "blank"
;;
esac
}
# Check if fzf is installed
if command -v fzf >/dev/null 2>&1; then
# Use fzf to select a database or create a new one
SELECTED_DB=$(echo -e "$DATABASES\n[Create New Database]" | fzf --height=10 --layout=reverse --prompt="Select a database or create a new one: ")
if [ "$SELECTED_DB" = "[Create New Database]" ]; then
prompt_new_db
elif [ -n "$SELECTED_DB" ]; then
update_env "$SELECTED_DB"
else
echo "No database selected. No changes made."
fi
else
echo "fzf is not installed. Please provide the database name as an argument, or use 'new' to create a new database."
if [ "$1" = "new" ]; then
prompt_new_db
elif [ -n "$1" ]; then
if echo "$DATABASES" | grep -q "^$1$"; then
update_env "$1"
else
echo "Error: Database '$1' not found in the list of available databases."
echo "Available databases:"
echo "$DATABASES"
echo "Use 'new' to create a new database."
fi
else
echo "No database name provided. Available databases:"
echo "$DATABASES"
echo "Use 'new' to create a new database."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment