PowerShell_Scripts/docker/Edit-DockerCompose.ps1

55 lines
1.8 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ProjectPath=@($PWD),
[switch]$Force,
[string[]]$AdditionalFiles
)
$local:EditCandidates = @('docker-compose.###','.base.docker-compose.###')
$local:EditBonus = @('Dockerfile', '.env', '*.env')
$local:editFiles = @()
foreach( $local:ext in @('.yml', '.yaml') ) {
$local:rawEditCandidates = $EditCandidates | ForEach-Object { $_ -replace '\.###',$ext }
foreach( $local:fileName in $rawEditCandidates ) {
foreach( $local:path in $ProjectPath ) {
$local:testPath = Join-Path $path $fileName
if( $Force -or $(Test-Path $testPath) ) {
$editFiles += $testPath
$editFiles += Get-ChildItem $(Join-Path $path '*') -Include $EditBonus -Depth 1 |
Select-Object -ExpandProperty FullName
}
}
}
if( $editFiles ) {
break
}
}
$AdditionalFiles = $AdditionalFiles | Where-Object { $_ } | ForEach-Object {
$_ -replace
'^#lsi$','#/*linuxserver.yml' -replace
'^#tr?$','#/*traefik.yml' -replace
'^#/',"$env:SZ_DOCKER_SYSTEM_PATH"
} |
Get-ChildItem -Force |
Select-Object -ExpandProperty FullName
$local:FinalEditList = $editFiles | Where-Object {$_} | Sort-Object {
$local:n = [System.IO.Path]::GetFileName($_)
Join-Path [PSystem.IO.ath]::GetDirectoryName($_) "$(if( $n[0] -eq '.' ) { "9" } else { "1" })$n"
}
$FinalEditList = @() + $FinalEditList + @() + $AdditionalFiles
Edit-TextFile $FinalEditList
$editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q }