=Added Switch-DockerCompose

This commit is contained in:
lksz 2021-09-11 21:49:09 -04:00
parent 33b4c5166b
commit c395c28e01
2 changed files with 66 additions and 0 deletions

1
docker/Aliases/dcsw.ps1 Normal file
View File

@ -0,0 +1 @@
Switch-DockerCompose

View File

@ -0,0 +1,65 @@
[CmdletBinding(SupportsShouldProcess)]param(
[Parameter(ParameterSetName="On",Mandatory)]
[switch]$On,
[Parameter(ParameterSetName="Off",Mandatory)]
[switch]$Off,
[Parameter(ParameterSetName="Query",Mandatory)]
[switch]$NoChange,
[switch]$Recurse,
[int]$Depth=1,
[Parameter(ParameterSetName="On",Position = 0, ValueFromRemainingArguments)]
[Parameter(ParameterSetName="Off",Position = 0, ValueFromRemainingArguments)]
[Parameter(ParameterSetName="Toggle",Position = 0, ValueFromRemainingArguments)]
[Parameter(ParameterSetName="Query",Position = 0, ValueFromRemainingArguments)]
[ArgumentCompleter({Invoke-Command -ScriptBlock $_DockerComposeDirsCompleter -ArgumentList $args})]
[string[]]$ProjectPath
)
if( -not $ProjectPath ) { $ProjectPath=@($PWD) }
$local:ext = ".yml"
$local:dcPathList = @()
$ProjectPath | ForEach-Object {
while( $true ) {
if( (Test-Path (Join-Path $_ "docker-compose$ext")) -and -not $Recurse ) {
$dcPathList += Resolve-Path $_
} elseif( $Recurse ) {
$local:gciParams = [ordered]@{
Recurse = $true
}
if( $Depth ) {
$gciParams.Depth = $Depth
}
$dcPathList += Get-ChildItem -Path $_ -Include "docker-compose$ext" @gciParams |
Select-Object -ExpandProperty Directory # |
# Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) }
}
if( ($dcPathList -and -not $Recure) -or $ext -eq '.yaml' ) {
break
}
$ext = '.yaml'
}
}
if( -not $dcPathList ) { return }
$dcPathList | ForEach-Object {
$local:NoAutoPath = $(Join-Path $_ '.noauto.all')
$local:NoAutoOffPath = "${NoAutoPath}.off"
$local:IsNoAuto = Test-Path $NoAutoPath
$local:IsNoAutoOff = Test-Path $NoAutoOffPath
if( ($NoChange -and $IsNoAuto) -or (-not $NoChange -and ($Off -or (-not $On -and -not $IsNoAuto)) ) ) {
if( $IsNoAutoOff ) {
Move-Item $NoAutoOffPath $NoAutoPath
} elseif( -not $IsNoAuto -and -not $NoChange ) {
$null = New-Item -ItemType File -Path $NoAutoPath -Value ""
}
Write-Host -ForegroundColor Magenta -NoNewline "Off"
} elseif( $NoChange -or $On -or (-not $Off -and $IsNoAuto) ) {
if( $IsNoAuto -and -not $NoChange ) {
Move-Item $NoAutoPath $NoAutoOffPath
}
Write-Host -ForegroundColor Gree -NoNewline "On "
}
Write-Host -ForegroundColor Cyan " $("$_".Replace($PWD.Path,'').Substring(1))"
}