#!/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!"