PowerShell_Scripts/docker/Edit-DockerCompose.ps1

55 lines
2.0 KiB
PowerShell
Raw Normal View History

[CmdletBinding(SupportsShouldProcess)]param(
2021-03-23 05:06:49 +00:00
[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
2021-01-24 03:07:36 +00:00
dcc -ProjectPath $ProjectPath -TestOnly