-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathinstall_pocketbase.ps1
120 lines (101 loc) · 4.07 KB
/
install_pocketbase.ps1
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
# 1. Check if pocketbase exists
function Check-PocketBase {
if (Test-Path ".\pb\pocketbase.exe") {
Write-Host "Detected ./pb/pocketbase already exists, please delete it manually and try again" -ForegroundColor Red
exit 1
}
if (-not (Test-Path ".\pb")) {
New-Item -ItemType Directory -Path ".\pb"
}
}
# 2. Get available versions
function Get-PocketBaseVersions {
Write-Host "Fetching available versions..." -ForegroundColor Green
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/pocketbase/pocketbase/releases"
$global:VERSIONS = $response | ForEach-Object { $_.tag_name }
$global:LATEST_VERSION = $VERSIONS[0]
}
# 3. Select version with arrow keys
function Select-PocketBaseVersion {
Clear-Host
$current = 0
$total = $VERSIONS.Count
while ($true) {
Clear-Host
Write-Host "Available versions (Use ↑↓ arrows to select, Enter to confirm):" -ForegroundColor Yellow
Write-Host "----------------------------------------"
for ($i = 0; $i -lt $total; $i++) {
if ($i -eq $current) {
Write-Host ("-> " + $VERSIONS[$i]) -ForegroundColor Green
} else {
Write-Host (" " + $VERSIONS[$i])
}
}
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
switch ($key.VirtualKeyCode) {
38 { # Up arrow
if ($current -gt 0) { $current-- }
}
40 { # Down arrow
if ($current -lt ($total - 1)) { $current++ }
}
13 { # Enter
$global:SELECTED_VERSION = $VERSIONS[$current]
Write-Host "`nSelected version: $SELECTED_VERSION" -ForegroundColor Green
return
}
}
}
}
# 4. Download PocketBase
function Download-PocketBase {
$versionNum = $SELECTED_VERSION -replace '^v'
$fileName = "pocketbase_${versionNum}_windows_amd64.zip"
$downloadUrl = "https://github.com/pocketbase/pocketbase/releases/download/$SELECTED_VERSION/$fileName"
$outputPath = ".\pb\pocketbase.zip"
Write-Host "Downloading PocketBase $SELECTED_VERSION..." -ForegroundColor Green
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputPath
Write-Host "Extracting files..." -ForegroundColor Green
Expand-Archive -Path $outputPath -DestinationPath ".\pb" -Force
Remove-Item $outputPath
}
# 5. Configure admin account
function Configure-AdminAccount {
Write-Host "`nConfiguring admin account" -ForegroundColor Yellow
do {
$email = Read-Host "Enter admin email"
} while (-not ($email -match "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
do {
$password = Read-Host "Enter admin password (min 8 chars)" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$passwordText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
} while ($passwordText.Length -lt 8)
$global:ADMIN_EMAIL = $email
$global:ADMIN_PASSWORD = $passwordText
}
# 6. Configure environment file
function Configure-Environment {
if (-not (Test-Path ".\core\.env")) {
Copy-Item "env_sample" -Destination ".\core\.env"
Write-Host "Created new .env file from template" -ForegroundColor Green
} else {
Write-Host "Found existing .env file" -ForegroundColor Yellow
}
$envContent = Get-Content ".\core\.env"
$envContent = $envContent -replace 'export PB_API_AUTH="[^"]*"', "export PB_API_AUTH=`"$ADMIN_EMAIL|$ADMIN_PASSWORD`""
Set-Content ".\core\.env" $envContent
Write-Host "Updated PB_API_AUTH in .env with new credentials" -ForegroundColor Green
}
# Main execution
function Main {
Write-Host "Starting PocketBase installation..." -ForegroundColor Cyan
Check-PocketBase
Get-PocketBaseVersions
Select-PocketBaseVersion
Download-PocketBase
Configure-AdminAccount
Configure-Environment
Write-Host "PocketBase installation completed!" -ForegroundColor Green
}
# Run the script
Main