Docker.psm1
Function Set-ModuleDocker { ForEach ($Command in @('docker', 'docker-compose')) { $Invoke = If ($Command -eq 'docker') { 'Invoke-DockerCommand' } Else { 'Invoke-DockerComposeCommand' } Set-Alias -Name $Command -Value $Invoke -Scope Global -ErrorAction Stop If (-Not (Test-Path -PathType Leaf -Path $Profile)) { New-Item -ItemType File -Path $Profile } $Content = "Set-Alias -Name $Command -Value $Invoke" If (-Not (Get-Content -Path $Profile | Select-String -SimpleMatch $Content)) { Add-Content -Path $Profile -Value $Content } } } Function Mount-VirtualHosts { Param( [Parameter(Mandatory = $True)] $Command, $Arguments ) If ($Command -eq 'docker') { $Content = [String] $Arguments $Pattern = '-e\s+VIRTUAL_HOST=(.+)\s*' } If ($Command -eq 'docker-compose') { $File = 'docker-compose.yml' If ($MatchFile = [String] $Arguments | Select-String -Pattern "-f\s+(.+)\s*") { $File = ((($MatchFile.Matches[0].Groups[1].Value) -Replace "'", '') -Replace '"', '') } $Content = Get-Content -Path $File $Pattern = '^\s*VIRTUAL_HOST\s*[=:]\s*(.+)\s*$' } If ($MatchHosts = $Content | Select-String -Pattern $Pattern) { Foreach ($VirtualHost in (((($MatchHosts.Matches[0].Groups[1].Value) -Replace "'", '') -Replace '"', '') -Split ',')) { New-HostnameMapping -Hostname $VirtualHost } } } Function Invoke-DockerCommand { Mount-VirtualHosts -Command docker -Arguments $Args Invoke-Expression -Command "Docker $Args" } Function Invoke-DockerComposeCommand { Mount-VirtualHosts -Command docker-compose -Arguments $Args Invoke-Expression -Command "Docker-Compose $Args" } |