$ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot $secrets = Join-Path $root 'secrets' $passwordPath = Join-Path $secrets 'postgres_password' $databaseUrlPath = Join-Path $secrets 'database_url' if (-not (Test-Path (Join-Path $root '.env'))) { Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env') } $hasPassword = Test-Path $passwordPath $hasDatabaseUrl = Test-Path $databaseUrlPath if ($hasPassword -or $hasDatabaseUrl) { if ($hasPassword -and $hasDatabaseUrl) { Write-Output 'Local secrets already exist; leaving them unchanged.' exit 0 } throw 'The local secret set is incomplete. Restore both files from the same setup; do not rotate a password against an existing volume.' } New-Item -ItemType Directory -Force $secrets | Out-Null $bytes = New-Object byte[] 32 $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() try { $rng.GetBytes($bytes) } finally { $rng.Dispose() } $builder = New-Object System.Text.StringBuilder foreach ($byte in $bytes) { [void]$builder.Append($byte.ToString('x2')) } $password = $builder.ToString() [System.IO.File]::WriteAllText($passwordPath, $password, [System.Text.Encoding]::ASCII) $databaseUrl = 'postgresql://messenger:' + $password + '@postgres:5432/messenger' [System.IO.File]::WriteAllText($databaseUrlPath, $databaseUrl, [System.Text.Encoding]::ASCII) Write-Output 'Created local Compose secrets. Their values were not printed.'