Drude.psm1

<# Copyright (C) Alex Danilenko. All rights reserved. #>

requires -version 3.0

$drudeMessages = @{
    ComposeFileNotFound = "Sorry, but docker-compose.yml file is not found in current directory."
    ComposeFileFound = "docker-compose.yml file found in current directory."
}

Function Check-ForDockerComposeFile(){
    $file_found = Test-Path ".\docker-compose.yml";
    
    if($file_found -eq $false){
        Write-Host -ForegroundColor Red -Object $drudeMessages.ComposeFileNotFound
    }

    return $file_found
}

# ================================================================================================== #

Function Start-Drude(){
    [cmdletbinding()]
    [Alias("dsh-up")]
    param (
      [Parameter(Position=0)][string]$cliContainer = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Green -Object "Starting all containers..."

        docker-compose up -d

        Write-Host -ForegroundColor Green -Object "Reseting permissions on /var/www in $cliContainer container..."
        docker exec -u root $(docker-compose ps -q $cliContainer) bash -c "chown -R docker:users /var/www"
    }
}

# ================================================================================================== #

Function Stop-Drude(){
    [cmdletbinding()]
    
    param ()

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Green -Object "Stopping all containers..."
        docker-compose stop
    }
}

# ================================================================================================== #

Function Restart-Drude(){
    [cmdletbinding()]
    
    param ()

    Stop-Drude
    Start-Drude
}

# ================================================================================================== #

Function Get-DrudeStatus(){
    [cmdletbinding()]
    
    param (
        [Parameter(Position=0)][string]$container = ""
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker-compose ps $container
    }
}

# ================================================================================================== #

Function Invoke-DrudeBash(){
    [cmdletbinding()]
    
    param (
        [Parameter(Position=0)][string]$container = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker exec -it $(docker-compose ps -q $container) bash
    }
}

# ================================================================================================== #

Function Invoke-DrudeBashCommand(){
    [cmdletbinding()]
    
    param (
        [Parameter(Position=0)][string]$container = "cli",
        [Parameter(Position=1,Mandatory=$true)][string]$command = "ls -la"
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker exec -it $(docker-compose ps -q $container) bash -c "$command"
    }
}

# ================================================================================================== #

Function Invoke-DrudeDrushCommand(){
    [cmdletbinding()]
    
    param (
          [Parameter(Position=0)][string]$command = "status",
          [Parameter(Position=1)][string]$site = "default",
          [Parameter(Position=2)][string]$docroot = "/var/www/docroot",
          [Parameter(Position=3)][string]$cliContainer = "cli"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan   -Object "Executing"
        Write-Host -ForegroundColor Yellow -Object "$drush $command" 
        Write-Host -ForegroundColor Cyan   -Object "in $docroot/sites/$site folder of $cliContainer container..."
        docker exec -it $(docker-compose ps -q $cliContainer) bash -c "cd $docroot && cd sites/$site && drush $command"
    }
}

# ================================================================================================== #

Function Get-DrudeLogs(){
    [cmdletbinding()]
    
    param (
        [Parameter(Position=0)][string]$container = ""
    )

    if(Check-ForDockerComposeFile -eq $true){
        docker-compose logs -f $container
    }
}

# ================================================================================================== #

Function Clear-Drude(){
    [cmdletbinding()]
    
    param (
      [string]$arguments = "--remove-orphans"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
        Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
            "Deletes all containers and their contents."

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
            "Keeps all as it is."

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0) 

        switch ($result){
          0 {
            docker-compose down $arguments
          }
        }
    }
}

Function Reset-Drude(){
    [cmdletbinding()]
    
    param (
      [string]$arguments = "--remove-orphans"
    )

    if(Check-ForDockerComposeFile -eq $true){
        Write-Host -ForegroundColor Cyan -Object "You are going to remove ALL CONTAINERS and their contents (like database tables, caches, manually installed packages, etc.)."
        Write-Host -ForegroundColor Red -Object "This operation cannot be undone and may result to lost of data!"

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
            "Deletes all containers and their contents."

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
            "Keeps all as it is."

        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $host.ui.PromptForChoice($title, "Are you sure?", $options, 0) 

        switch ($result){
          0 {
            docker-compose down $arguments
            Start-Drude
          }
        }
    }
}

Microsoft.PowerShell.Core\Export-ModuleMember -Function "Start-Drude"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Stop-Drude"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Restart-Drude"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Get-DrudeStatus"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Get-DrudeLogs"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Invoke-DrudeBash"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Invoke-DrudeBashCommand"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Invoke-DrudeDrushCommand"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Clear-Drude"
Microsoft.PowerShell.Core\Export-ModuleMember -Function "Reset-Drude"

New-Alias -Name dsh-up         -value "Start-Drude"
New-Alias -Name dsh-down       -value "Stop-Drude"
New-Alias -Name dsh-stop       -value "Stop-Drude"
New-Alias -Name dsh-restart    -value "Restart-Drude"
New-Alias -Name dsh-status     -value "Get-DrudeStatus"
New-Alias -Name dsh-ps         -value "Get-DrudeStatus"
New-Alias -Name dsh-logs       -value "Get-DrudeLogs"
New-Alias -Name dsh-bash       -value "Invoke-DrudeBash"
New-Alias -Name dsh-exec       -value "Invoke-DrudeBashCommand"
New-Alias -Name dsh-drush      -value "Invoke-DrudeDrushCommand"
New-Alias -Name dsh-reset      -value "Reset-Drude"

export-modulemember -alias * -function *