PowerShell_Scripts/docker/Switch-DockerCompose.ps1

90 lines
3.3 KiB
PowerShell
Raw Normal View History

2021-09-12 01:49:09 +00:00
[CmdletBinding(SupportsShouldProcess)]param(
[Parameter(ParameterSetName="On",Mandatory)]
[switch]$On,
[Parameter(ParameterSetName="Off",Mandatory)]
[switch]$Off,
2021-09-24 01:56:01 +00:00
[Parameter(ParameterSetName="Toggle",Mandatory)]
[switch]$Toggle,
2021-09-12 01:49:09 +00:00
[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 |
2021-11-29 22:00:43 +00:00
Select-Object -ExpandProperty Directory
2021-09-12 01:49:09 +00:00
}
if( ($dcPathList -and -not $Recure) -or $ext -eq '.yaml' ) {
break
}
$ext = '.yaml'
}
}
if( -not $dcPathList ) { return }
2021-09-24 01:56:01 +00:00
$local:QueryOnly = -not $On -and -not $Off -and -not $Toggle
2021-09-12 01:49:09 +00:00
$dcPathList | ForEach-Object {
2021-09-24 01:56:01 +00:00
$local:name = "$_"
2021-11-29 22:00:43 +00:00
$local:c = Get-Content -Head 3 (Join-Path $_ "docker-compose$ext")
$local:r = [ordered]@{
Dir = Split-Path -Leaf $_
State = ""
Version = ($c[0] -replace 'Version: ','' -replace "[`"']",'').Trim()
Stack = "?"
}
if( $c[1] -match "^# stack(?:\W*): ?(\w+)\W*$" ) { $r.Stack = $matches[1] }
if( $c[2] -match "^# ports prefix: ([\dx#]{4,5})" ) { $r.Stack = $matches[1] + " | " + $r.Stack }
2021-09-24 01:56:01 +00:00
if( $PWD -like $name ) {
$name = Split-Path -Leaf $name
} else {
$name = $($name.Replace($PWD.Path,'').Substring(1))
}
2021-09-12 01:49:09 +00:00
$local:NoAutoPath = $(Join-Path $_ '.noauto.all')
$local:NoAutoOffPath = "${NoAutoPath}.off"
2021-09-24 01:56:01 +00:00
$local:IsOff = Test-Path $NoAutoPath
$local:IsExplicitOn = Test-Path $NoAutoOffPath
2021-09-12 01:49:09 +00:00
2021-09-24 01:56:01 +00:00
$local:SetOff = $Off -or ($Toggle -and -not $IsOff)
$local:SetOn = $On -or ($Toggle -and $IsOff)
if( ($QueryOnly -and $IsOff) -or (-not $QueryOnly -and $SetOff) ) {
if( -not $QueryOnly -and $IsExplicitOn ) {
2021-09-12 01:49:09 +00:00
Move-Item $NoAutoOffPath $NoAutoPath
2021-09-24 01:56:01 +00:00
} elseif( -not $QueryOnly -and -not $IsOff ) {
2021-09-12 01:49:09 +00:00
$null = New-Item -ItemType File -Path $NoAutoPath -Value ""
}
2021-11-29 22:00:43 +00:00
# Write-Host -ForegroundColor Magenta -NoNewline "Off"
$r.State = "Off"
2021-09-24 01:56:01 +00:00
} elseif( ($QueryOnly -and -not $IsOff) -or (-not $QueryOnly -and $SetOn) ) {
if( -not $QueryOnly -and $IsOff ) {
2021-09-12 01:49:09 +00:00
Move-Item $NoAutoPath $NoAutoOffPath
}
2021-11-29 22:00:43 +00:00
# Write-Host -ForegroundColor Green -NoNewline "On "
$r.State = "On"
2021-09-12 01:49:09 +00:00
}
2021-11-29 22:00:43 +00:00
# Write-Host -ForegroundColor Cyan " $name"
[PSCustomObject]$r
2021-09-12 01:49:09 +00:00
}