- 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.
64 lines
1.5 KiB
Bash
Executable File
64 lines
1.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Simple Bitwarden .env Sync
|
|
# Works on Mac and Windows (Git Bash)
|
|
|
|
echo "=== Bitwarden .env Sync ==="
|
|
|
|
# Check if bw is installed
|
|
if ! command -v bw &> /dev/null; then
|
|
echo "Error: Bitwarden CLI (bw) is not installed"
|
|
echo "Install it from: https://bitwarden.com/help/cli/"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if logged in
|
|
STATUS=$(bw status | grep -o '"status":"[^"]*' | cut -d'"' -f4)
|
|
|
|
if [ "$STATUS" = "unauthenticated" ]; then
|
|
echo "Please login to Bitwarden:"
|
|
bw login
|
|
STATUS="locked"
|
|
fi
|
|
|
|
# Unlock vault if needed
|
|
if [ "$STATUS" = "locked" ]; then
|
|
echo "Unlocking vault..."
|
|
export BW_SESSION=$(bw unlock --raw)
|
|
if [ $? -ne 0 ]; then
|
|
echo "Error: Failed to unlock vault"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Sync with Bitwarden
|
|
echo "Syncing with Bitwarden..."
|
|
bw sync
|
|
|
|
# CHANGE THIS to your Bitwarden item name
|
|
ITEM_NAME="portfolio-env"
|
|
|
|
echo "Fetching environment variables..."
|
|
|
|
# Get the item from Bitwarden
|
|
ITEM=$(bw get item "$ITEM_NAME" 2>/dev/null)
|
|
|
|
if [ -z "$ITEM" ]; then
|
|
echo "Error: Could not find item '$ITEM_NAME' in Bitwarden"
|
|
echo "Make sure you have an item with this exact name in your vault"
|
|
exit 1
|
|
fi
|
|
|
|
# Extract notes (where env vars should be stored)
|
|
NOTES=$(echo "$ITEM" | grep -o '"notes":"[^"]*' | cut -d'"' -f4 | sed 's/\\n/\n/g')
|
|
|
|
if [ -z "$NOTES" ]; then
|
|
echo "Error: No notes found in item '$ITEM_NAME'"
|
|
echo "Add your environment variables to the notes field"
|
|
exit 1
|
|
fi
|
|
|
|
# Create .env file
|
|
echo "$NOTES" > .env
|
|
|
|
echo "✓ Created .env file successfully!" |