PowerShell_Scripts/docker/Edit-DockerCompose.ps1

55 lines
2.0 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({Invoke-Command -ScriptBlock $_DockerComposeDirsCompleter -ArgumentList $args})]
[string[]]$ProjectPath=@($PWD),
[switch]$Force,
[string[]]$AdditionalFiles
)
$local:EditCandidates = @('docker-compose.###','.base.docker-compose.###','secrets.env')
$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 | Get-Path
$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($_)
$local:sorder = "$(
if( $n[0] -eq '.' ) { "2" }
elseif( $n -match 'docker-compose\.y' ) { "1" }
else { "9" }
)$n"
Write-Verbose "sorder($n)=$sorder"
$local:sorder = Join-Path $([System.IO.Path]::GetDirectoryName($_)) $sorder
$sorder
}
$FinalEditList = (@() + $FinalEditList + @() + $AdditionalFiles) | Select-Object -Unique
Write-Verbose "`$FinalEditList = `n$($FinalEditList | Out-String)"
Edit-TextFile $FinalEditList
dcc -ProjectPath $ProjectPath -TestOnly