How to Use Copilot


# PowerShell script for Windows
# Blinko installation script

# Colors for better visibility
$RED = "Red"
$GREEN = "Green"
$YELLOW = "Yellow"

# Function to write colored output
function Write-ColorOutput {
    param(
        [string]$Message,
        [string]$Color = "White"
    )
    Write-Host $Message -ForegroundColor $Color
}

# Check if Docker is running
try {
    docker --version | Out-Null
    if ($LASTEXITCODE -ne 0) {
        throw "Docker not found"
    }
} catch {
    Write-ColorOutput "Docker is not installed or not running. Please install Docker Desktop and ensure it's running." $RED
    exit 1
}

# Step 1: Check if the network 'blinko-network' already exists
$networkExists = docker network ls --format "{{.Name}}" | Where-Object { $_ -eq "blinko-network" }

if (-not $networkExists) {
    Write-ColorOutput "Network 'blinko-network' does not exist. Creating network..." $YELLOW
    docker network create blinko-network
    
    if ($LASTEXITCODE -ne 0) {
        Write-ColorOutput "Failed to create Docker network. Please check your Docker setup." $RED
        exit 1
    }
    Write-ColorOutput "Successfully created Docker network: blinko-network" $GREEN
} else {
    Write-ColorOutput "Network 'blinko-network' already exists. Skipping network creation." $YELLOW
}

# Step 2: Check if the PostgreSQL container 'blinko-postgres' already exists
$containerExists = docker ps -a --format "{{.Names}}" | Where-Object { $_ -eq "blinko-postgres" }

if ($containerExists) {
    Write-ColorOutput "Container 'blinko-postgres' already exists. Skipping container creation." $YELLOW
} else {
    Write-ColorOutput "2. 🐳 Starting PostgreSQL container..." $YELLOW
    docker run -d `
      --name blinko-postgres `
      --network blinko-network `
      -p 5435:5432 `
      -e POSTGRES_DB=postgres `
      -e POSTGRES_USER=postgres `
      -e POSTGRES_PASSWORD=mysecretpassword `
      -e TZ=Asia/Shanghai `
      --restart always `
      postgres:14

    if ($LASTEXITCODE -ne 0) {
        Write-ColorOutput "Failed to start PostgreSQL container." $RED
        exit 1
    }
    Write-ColorOutput "✅ PostgreSQL container is running." $GREEN
}

# Step 3: Prompt user to optionally mount the .blinko directory
Write-ColorOutput "Do you want to mount a local '.blinko' directory to '/app/.blinko' in the container? (y/n)" $YELLOW
$mountChoice = Read-Host "Enter your choice"

$volumeMount = ""
if ($mountChoice -eq "y" -or $mountChoice -eq "Y") {
    $blinkoFolder = Read-Host "Please provide the path to your '.blinko' folder"
    
    # Convert to absolute path if relative
    if (-not [System.IO.Path]::IsPathRooted($blinkoFolder)) {
        $blinkoFolder = Join-Path (Get-Location) $blinkoFolder
    }
    
    # Check if the directory exists; if not, create it
    if (-not (Test-Path $blinkoFolder)) {
        Write-ColorOutput "Directory does not exist. Creating directory..." $YELLOW
        try {
            New-Item -ItemType Directory -Path $blinkoFolder -Force | Out-Null
        } catch {
            Write-ColorOutput "Failed to create the directory. Please check permissions." $RED
            exit 1
        }
    }
    
    # Check if the directory has write permissions
    try {
        $testFile = Join-Path $blinkoFolder "test_write_permission.tmp"
        New-Item -ItemType File -Path $testFile -Force | Out-Null
        Remove-Item $testFile -Force
    } catch {
        Write-ColorOutput "The directory '$blinkoFolder' does not have write permissions." $RED
        exit 1
    }
    
    Write-ColorOutput "Directory is ready for mounting: $blinkoFolder" $GREEN
    # Convert Windows path to Docker-compatible format
    $dockerPath = $blinkoFolder -replace '\\', '/' -replace '^([A-Za-z]):', '//$1'
    $volumeMount = "-v `"${dockerPath}:/app/.blinko`""
} else {
    Write-ColorOutput "Skipping mounting of .blinko directory." $YELLOW
}

# Step 4: Run BlinkOS container with or without volume path
Write-ColorOutput "3. 🖥️ Starting BlinkOS container..." $YELLOW

# Build the docker command
$dockerCmd = @(
    "docker", "run", "-d",
    "--name", "blinko-website",
    "--network", "blinko-network",
    "-p", "1111:1111",
    "-e", "NODE_ENV=production",
    "-e", "NEXTAUTH_SECRET=my_ultra_secure_nextauth_secret",
    "-e", "DATABASE_URL=postgresql://postgres:mysecretpassword@blinko-postgres:5432/postgres"
)

if ($volumeMount) {
    $dockerCmd += $volumeMount.Split(' ')
}

$dockerCmd += @("--restart", "always", "blinkospace/blinko:latest")

# Execute the docker command
& $dockerCmd[0] $dockerCmd[1..($dockerCmd.Length-1)]

if ($LASTEXITCODE -ne 0) {
    Write-ColorOutput "Failed to start BlinkOS container." $RED
    exit 1
}

Write-ColorOutput "✅ All containers are up and running." $GREEN
Write-ColorOutput "You can access Blinko at: http://localhost:1111" $GREEN