- Introduced `sync-env.ps1` to facilitate the synchronization of environment variables from Bitwarden on Windows.
- Implemented checks for Bitwarden CLI installation and authentication status.
- Added functionality to fetch environment variables from a specified Bitwarden item and create/update a `.env` file.
- Enhanced user feedback with clear error messages and success confirmations.
✅ This script streamlines the management of environment variables by integrating with Bitwarden, ensuring secure and efficient updates.
62 lines
1.6 KiB
PowerShell
62 lines
1.6 KiB
PowerShell
# Simple Bitwarden .env Sync for Windows
|
|
# Run with: powershell -ExecutionPolicy Bypass -File sync-env.ps1
|
|
|
|
Write-Host "=== Bitwarden .env Sync ===" -ForegroundColor Cyan
|
|
|
|
# Check if bw is installed
|
|
if (!(Get-Command bw -ErrorAction SilentlyContinue)) {
|
|
Write-Host "Error: Bitwarden CLI (bw) is not installed" -ForegroundColor Red
|
|
Write-Host "Install it from: https://bitwarden.com/help/cli/"
|
|
exit 1
|
|
}
|
|
|
|
# Check status
|
|
$status = bw status | ConvertFrom-Json
|
|
|
|
if ($status.status -eq "unauthenticated") {
|
|
Write-Host "Please login to Bitwarden:"
|
|
bw login
|
|
$status = bw status | ConvertFrom-Json
|
|
}
|
|
|
|
# Unlock if needed
|
|
if ($status.status -eq "locked") {
|
|
Write-Host "Unlocking vault..."
|
|
$env:BW_SESSION = bw unlock --raw
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Error: Failed to unlock vault" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Sync
|
|
Write-Host "Syncing with Bitwarden..."
|
|
bw sync | Out-Null
|
|
|
|
# CHANGE THIS to your Bitwarden item name
|
|
$itemName = "portfolio-env"
|
|
|
|
Write-Host "Fetching environment variables..."
|
|
|
|
# Get item
|
|
$item = bw get item $itemName 2>$null | ConvertFrom-Json
|
|
|
|
if (!$item) {
|
|
Write-Host "Error: Could not find item '$itemName' in Bitwarden" -ForegroundColor Red
|
|
Write-Host "Make sure you have an item with this exact name in your vault"
|
|
exit 1
|
|
}
|
|
|
|
# Get notes
|
|
$notes = $item.notes
|
|
|
|
if (!$notes) {
|
|
Write-Host "Error: No notes found in item '$itemName'" -ForegroundColor Red
|
|
Write-Host "Add your environment variables to the notes field"
|
|
exit 1
|
|
}
|
|
|
|
# Create .env file
|
|
$notes | Out-File -FilePath ".env" -Encoding UTF8 -NoNewline
|
|
|
|
Write-Host "✓ Created .env file successfully!" -ForegroundColor Green |