Skip to content

Commit

Permalink
Update publish-krewplugin.yaml to insert new param block and remove e…
Browse files Browse the repository at this point in the history
…xisting param block from Invoke-KubeTidy function

Signed-off-by: PixelRobots <22979170+PixelRobots@users.noreply.github.com>
  • Loading branch information
PixelRobots committed Oct 1, 2024
1 parent c53d876 commit a16dedb
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions .github/workflows/publish-krewplugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,42 @@ jobs:
# Read content and update it
$content = Get-Content $destinationFile -Raw
# Split the content into lines
$lines = $content -split "`n"

# Find the shebang line and insert the new param block
$shebangLineIndex = $lines.IndexOf('#!/usr/bin/env pwsh')
if ($shebangLineIndex -ne -1) {
# Insert the new param block right after the shebang line
$lines = $lines[0..$shebangLineIndex] + $paramBlock -split "`n" + $lines[$shebangLineIndex + 1..$lines.Length]
}
# Find the Invoke-KubeTidy function definition
$invokeFunctionIndex = $lines.IndexOf('function Invoke-KubeTidy {')
if ($invokeFunctionIndex -ne -1) {
# Remove or comment out the existing param block within the function
for ($i = $invokeFunctionIndex + 1; $i -lt $lines.Length; $i++) {
if ($lines[$i] -match 'param\s*\(') {
# Comment out the existing param block
while ($lines[$i] -ne '}') {
$lines[$i] = "# $($lines[$i])"
$i++
# Create a new array of lines with the new param block added
$newLines = @()
$newLines += $lines[0..$shebangLineIndex] # Add lines up to shebang
$newLines += $paramBlock -split "`n" # Add new param block
$newLines += $lines[$shebangLineIndex + 1..$lines.Length] # Add remaining lines

# Now remove the existing param block from the Invoke-KubeTidy function
$invokeFunctionIndex = $newLines.IndexOf('function Invoke-KubeTidy {')
if ($invokeFunctionIndex -ne -1) {
# Loop through lines to find and comment out/remove existing param block
for ($i = $invokeFunctionIndex + 1; $i -lt $newLines.Length; $i++) {
if ($newLines[$i] -match 'param\s*\(') {
# Comment out the existing param block
while ($newLines[$i] -ne '}') {
$newLines[$i] = "# $($newLines[$i])"
$i++
}
break
}
break
}
# If you want to remove instead of comment out, you can use:
# $lines[$i] = ''
}

# Ensure the last lines are correct
$newLines += "`n# Call the function, passing parameters manually for cross-platform compatibility"
$newLines += "`nInvoke-KubeTidy \$PSBoundParameters"

# Write the updated content back to the file
Set-Content -Path $destinationFile -Value $newLines -Force
}
# Ensure the last lines are correct
$lines += "`n# Call the function, passing parameters manually for cross-platform compatibility"
$lines += "`nInvoke-KubeTidy \$PSBoundParameters"
# Write the updated content back to the file
Set-Content -Path $destinationFile -Value $lines -Force


# # Update kubectl-KubeTidy.yaml with the new version
Expand Down

0 comments on commit a16dedb

Please sign in to comment.