Files
mtusi2026lab4/scripts/init-local.ps1
T

36 lines
1.2 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$envPath = Join-Path $repoRoot '.env'
if (-not (Test-Path -LiteralPath $envPath)) {
Copy-Item -LiteralPath (Join-Path $repoRoot '.env.example') -Destination $envPath
Write-Output 'Local .env created (non-secret session TTL).'
} else {
Write-Output 'Existing .env preserved.'
}
$secretsDirectory = Join-Path $repoRoot 'secrets'
$passwordPath = Join-Path $secretsDirectory 'postgres_password.txt'
New-Item -ItemType Directory -Path $secretsDirectory -Force | Out-Null
if (Test-Path -LiteralPath $passwordPath) {
if ((Get-Item -LiteralPath $passwordPath).Length -eq 0) {
throw 'Existing password file is empty. Supply a local password before starting PostgreSQL.'
}
Write-Output 'Existing PostgreSQL password preserved.'
exit 0
}
$randomBytes = New-Object byte[] 32
$generator = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$generator.GetBytes($randomBytes)
} finally {
$generator.Dispose()
}
$password = [Convert]::ToBase64String($randomBytes)
[System.IO.File]::WriteAllText($passwordPath, $password, [System.Text.Encoding]::ASCII)
Write-Output 'Local PostgreSQL password created in secrets/ (value not printed).'