docker and docker-compose commands
Invoke-DockerCompose which will be the basis of all other future dc prefixed commands (which currently contains to much copy-pasted code)
This commit is contained in:
parent
6e56ac6097
commit
24b5e928d0
14 changed files with 183 additions and 17 deletions
114
docker/Invoke-DockerCompose.ps1
Normal file
114
docker/Invoke-DockerCompose.ps1
Normal file
|
@ -0,0 +1,114 @@
|
|||
[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)) }
|
||||
|
||||
# _DIRS_=$( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -exec test -f {}/.ask.all \; -print | sort )
|
||||
# _DIRS_="$_DIRS_ $( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -not -exec test -f {}/.ask.all \; -not -exec test -f {}/.noauto.all \; -print | sort )"
|
||||
}
|
||||
}
|
||||
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 | 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 ) {
|
||||
CompleteJobs
|
||||
}
|
||||
|
||||
if( -not $dcParams ) { continue }
|
||||
|
||||
if( $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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CompleteJobs
|
||||
if( $CleanupJobQueue ) {
|
||||
$CleanupJobQueue | Remove-Job
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue