-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-turso-db.py
144 lines (120 loc) · 4.78 KB
/
generate-turso-db.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import subprocess
import re
import pyperclip
import argparse
import os
import sys
def run_command(command):
"""Run a shell command and return its output and error (if any)."""
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout.strip(), result.stderr.strip(), result.returncode
def update_env_file(file_path, new_vars):
if not os.path.exists(file_path):
print(f"File {file_path} does not exist. Creating new file.")
with open(file_path, 'w') as f:
f.write('\n'.join(f"{k}={v}" for k, v in new_vars.items()))
return
with open(file_path, 'r') as f:
lines = f.readlines()
updated_lines = []
updated = {key: False for key in new_vars.keys()}
for line in lines:
line = line.strip()
if line.startswith('#') or not line:
updated_lines.append(line)
continue
key = line.split('=')[0]
if key in new_vars:
if not updated[key]:
updated_lines.append(f'# Old {line}')
updated_lines.append(f'{key}={new_vars[key]}')
updated[key] = True
else:
updated_lines.append(line)
for key, value in new_vars.items():
if not updated[key]:
updated_lines.append(f'{key}={value}')
with open(file_path, 'w') as f:
f.write('\n'.join(updated_lines) + '\n')
def find_project_root():
"""Find the project root by looking for .git directory or pyproject.toml file."""
current_dir = os.getcwd()
while True:
if os.path.exists(os.path.join(current_dir, '.git')) or \
os.path.exists(os.path.join(current_dir, 'pyproject.toml')):
return current_dir
parent = os.path.dirname(current_dir)
if parent == current_dir:
return None
current_dir = parent
def check_turso_auth():
"""Check if the user is authenticated with Turso CLI."""
print("Checking Turso CLI authentication...")
output, error, code = run_command("turso auth status")
if code != 0 or "You are not logged in" in output:
print("Error: You are not authenticated with Turso CLI.")
print("Please run 'turso auth login' to authenticate.")
sys.exit(1)
print("Turso CLI authentication successful.")
# Set up argument parser
parser = argparse.ArgumentParser(description='Generate Turso DB credentials and update .env file.')
parser.add_argument('--overwrite', metavar='PATH', help='Path to .env or .env.local file to overwrite')
args = parser.parse_args()
# Find the project root directory
project_root = find_project_root()
if not project_root:
print("Error: Could not find project root. Make sure you're in a git repository or a directory with pyproject.toml.")
sys.exit(1)
# Check Turso CLI authentication
check_turso_auth()
# Create a new database
print("Creating new database...")
create_output, create_error, create_code = run_command("turso db create")
print(f"Output: {create_output}")
if create_error:
print(f"Error: {create_error}")
print(f"Return code: {create_code}")
sys.exit(1)
# Extract database name from create output
db_name_match = re.search(r'Created database (\S+)', create_output)
if db_name_match:
db_name = db_name_match.group(1)
else:
print("Error: Could not extract database name from output.")
sys.exit(1)
# Show database details
print(f"\nRetrieving details for database: {db_name}")
show_output, show_error, show_code = run_command(f"turso db show {db_name}")
print(f"Output: {show_output}")
if show_error:
print(f"Error: {show_error}")
print(f"Return code: {show_code}")
sys.exit(1)
# Extract URL from show output
url_match = re.search(r'URL:\s+(libsql://[\w.-]+)', show_output)
db_url = url_match.group(1) if url_match else "URL not found"
# Create a new token
print(f"\nCreating token for database: {db_name}")
token_output, token_error, token_code = run_command(f"turso db tokens create {db_name}")
print("Token created successfully")
if token_error:
print(f"Error: {token_error}")
print(f"Return code: {token_code}")
sys.exit(1)
# Get token from token output
auth_token = token_output.strip()
# Generate environment variables
new_vars = {"DB_URL": db_url, "AUTH_TOKEN": auth_token}
env_vars = f"DB_URL={db_url}\nAUTH_TOKEN={auth_token}"
# Print the environment variables to console
print("\nGenerated Environment Variables:")
print(env_vars)
if args.overwrite:
update_env_file(os.path.join(project_root, args.overwrite), new_vars)
print(f"\nEnvironment variables have been updated in {args.overwrite}")
else:
print("\nNo overwrite path provided. Environment variables will not be saved to a file.")
# Copy to clipboard
pyperclip.copy(env_vars)
print("Environment variables have been copied to clipboard. ")
print("Much love xxx remcostoeten")