
All dc... commands are now based on Invoke-DockerCompose. dco is aliased to Invoke-DockerCompose. dco_ is aliased to the docker-compose binary. '&' and '!' shorthand introduced to dco to execut in bg as job and wait for all bg jobs to complete respectively.
114 lines
3.5 KiB
PowerShell
114 lines
3.5 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess)]param(
|
|
[ArgumentCompleter({ param (
|
|
$commandName,
|
|
$parameterName,
|
|
$wordToComplete,
|
|
$commandAst,
|
|
$fakeBoundParameters
|
|
)
|
|
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
|
|
})]
|
|
[string[]]$ProjectPath=@($PWD),
|
|
[switch]$Recurse,
|
|
[int]$Depth=1,
|
|
[switch]$Force,
|
|
[switch]$OneLine,
|
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true, mandatory)]
|
|
[array]$CliParams
|
|
)
|
|
|
|
$script:JobQueue = @()
|
|
$script:CleanupJobQueue = @()
|
|
function CompleteJobs{
|
|
if( -not $JobQueue ) { return }
|
|
$local:currentJobQueue = $JobQueue
|
|
$JobQueue = @()
|
|
$CleanupJobQueue += $currentJobQueue
|
|
|
|
Wait-Job $currentJobQueue | ForEach-Object {
|
|
$_ | Receive-Job | ForEach-Object {
|
|
Write-Host "---: $($_.Path) | $($_.Params)" -ForegroundColor Cyan
|
|
$_.Results
|
|
}
|
|
}
|
|
Write-Host '--- Background jobs completed' -ForegroundColor Green
|
|
}
|
|
|
|
if( $CliParams[0] -is [string] ) {
|
|
$CliParams = @($CliParams,@($null))
|
|
}
|
|
|
|
$local:dcPathList = $ProjectPath | ForEach-Object {
|
|
if( (Test-Path (Join-Path $_ 'docker-compose.yml')) -and -not $Recurse ) {
|
|
Resolve-Path $_
|
|
} elseif( $Recurse ) {
|
|
$local:gciParams = [ordered]@{
|
|
Recurse = $true
|
|
}
|
|
if( $Depth ) {
|
|
$gciParams.Depth = $Depth
|
|
}
|
|
Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams |
|
|
Select-Object -ExpandProperty Directory |
|
|
Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) }
|
|
}
|
|
}
|
|
if( -not $dcPathList ) { return }
|
|
Write-Verbose "$dcPathList"
|
|
|
|
foreach( $local:p in $CliParams ) {
|
|
if( -not $p ) { continue }
|
|
|
|
$local:AsJob = $false;
|
|
$local:Wait = $false;
|
|
|
|
if( $p -isnot [String] ) {
|
|
$local:skipFirst = $true
|
|
switch( $p[0] ) {
|
|
'&' { $AsJob = $true }
|
|
'!' { $Wait = $true }
|
|
default { $skipFirst = $false }
|
|
}
|
|
if( $skipFirst ) { $p = $p | Select-Object -Skip 1 }
|
|
}
|
|
|
|
$dcParams = $p | Where-Object { $_ } | ForEach-Object {
|
|
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
|
|
else { "'$($_.Replace( "'", "''" ))'" }
|
|
}
|
|
|
|
$dcPathList | ForEach-Object {
|
|
$local:dcPath = Join-Path $_ 'docker-compose.yml'
|
|
if( -not (Test-Path $dcPath) ) { if( $Recurse ) { return } else { throw "ERROR: dcPath ($dcPath) does not exists" } }
|
|
|
|
Write-Verbose "& docker-compose --file $dcPath $dcParams"
|
|
if( $Wait -and -not $WhatIfPreference ) {
|
|
CompleteJobs
|
|
}
|
|
|
|
if( -not $dcParams ) { continue }
|
|
|
|
if( $WhatIfPreference ) {
|
|
Write-Host "In $dcPath Run $(if($AsJob){"(bg) "})with: $dcParams"
|
|
} elseif( $AsJob ) {
|
|
$JobQueue += Start-Job -ArgumentList @($dcPath,$dcParams) -ScriptBlock {
|
|
param($dcPath,$dcParams)
|
|
[PSCustomObject] $([ordered]@{
|
|
Path = $dcPath
|
|
Params = $dcParams
|
|
Results = $(& docker-compose --file $dcPath $dcParams)
|
|
})
|
|
}
|
|
} else {
|
|
& docker-compose --file $dcPath $dcParams
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
# if( $WhatIfPreference ) { return }
|
|
|
|
CompleteJobs
|
|
if( $CleanupJobQueue ) {
|
|
$CleanupJobQueue | Remove-Job
|
|
}
|