Skip to content

Commit

Permalink
Refactor script execution logic to handle multiple paths and recursiv…
Browse files Browse the repository at this point in the history
…e search for .ps1 files

Signed-off-by: PixelRobots <22979170+PixelRobots@users.noreply.github.com>
  • Loading branch information
PixelRobots committed Oct 28, 2024
1 parent 21446cd commit 425477d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.19] - 2024-10-28

### Changed
- **Re-added ASCII Art Banner**: The ASCII art banner has been reintroduced to the KubeTidy output after adjustments to ensure it displays correctly in the updated KubeDeck UI. This change improves compatibility and enhances the visual clarity of the output for users.

### Fixed
- **MacOS ARM Compatibility in Krew**: Updated the loading process for PowerShell modules to restore support for macOS ARM systems, similar to the compatibility achieved in version 0.0.14.

## [0.0.18] - 2024-10-22

### Changed
Expand Down
2 changes: 1 addition & 1 deletion KubeTidy.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'KubeTidy.psm1'

# Version number of this module.
ModuleVersion = '0.0.18'
ModuleVersion = '0.0.19'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
78 changes: 41 additions & 37 deletions KubeTidy.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,65 @@

# Dot Source all functions in all ps1 files located in this module
# Define the path to the local Private directory and Krew storage directory for KubeTidy
$localPrivateDir = "$PSScriptRoot\Private" # Local Private directory
$localPrivateDir = "$PSScriptRoot/Private" # Local Private directory
$krewStorageDir = "$HOME/.krew/store/kubetidy" # Krew storage directory

# Check if the local Private directory exists
if (Test-Path -Path $localPrivateDir) {
Write-Verbose "Executing scripts from local Private directory."
# Function to import all .ps1 scripts from a specified directory
function Import-Scripts {
param (
[string]$directory
)

# Get all .ps1 files in the local Private directory
$localScripts = Get-ChildItem -Path "$localPrivateDir/*.ps1" 2>$null
# Get all .ps1 files in the specified directory
$scripts = Get-ChildItem -Path "$directory/*.ps1" -ErrorAction SilentlyContinue

# Execute each .ps1 script found in the local Private directory
foreach ($script in $localScripts) {
# Execute each .ps1 script found in the directory
foreach ($script in $scripts) {
Write-Verbose "Executing script: $($script.FullName)"
. $script.FullName # Call the script
}
} else {
Write-Verbose "Local Private directory not found, checking Krew storage."
}

# Check if the KubeTidy storage directory exists
if (Test-Path -Path $krewStorageDir) {
Write-Verbose "Checking for available versions in $krewStorageDir."
# Check if the local Private directory exists and import scripts if it does
if (Test-Path -Path $localPrivateDir) {
Write-Verbose "Executing scripts from local Private directory."
Import-Scripts -directory $localPrivateDir
} else {
Write-Verbose "Local Private directory not found."
}

# Get all version directories (assuming they follow a vX.X.X naming pattern)
$versionDirs = Get-ChildItem -Path $krewStorageDir -Directory | Where-Object { $_.Name -match '^v\d+\.\d+\.\d+$' }
# Check if the KubeTidy storage directory exists
if (Test-Path -Path $krewStorageDir) {
Write-Verbose "Checking for available versions in $krewStorageDir."

# Check if any version directories were found
if ($versionDirs) {
# Get the latest version directory based on the version number
$latestVersionDir = $versionDirs | Sort-Object { [Version]$_.Name.Substring(1) } -Descending | Select-Object -First 1
# Get all version directories (assuming they follow a vX.X.X naming pattern)
$versionDirs = Get-ChildItem -Path $krewStorageDir -Directory | Where-Object { $_.Name -match '^v\d+\.\d+\.\d+$' }

Write-Verbose "Latest version found: $($latestVersionDir.Name)"
# Check if any version directories were found
if ($versionDirs) {
# Get the latest version directory based on the version number
$latestVersionDir = $versionDirs | Sort-Object { [Version]$_.Name.Substring(1) } -Descending | Select-Object -First 1

# Construct the path to the Private directory for the latest version
$kubePrivateDir = Join-Path -Path $latestVersionDir.FullName -ChildPath "Private"
Write-Verbose "Latest version found: $($latestVersionDir.Name)"

# Check if the Private directory exists in the latest version
if (Test-Path -Path $kubePrivateDir) {
# Get all .ps1 files in the Private directory
$scripts = Get-ChildItem -Path "$kubePrivateDir/*.ps1" 2>$null
# Construct the path to the Private directory for the latest version
$kubePrivateDir = Join-Path -Path $latestVersionDir.FullName -ChildPath "Private"

# Execute each .ps1 script found
foreach ($script in $scripts) {
Write-Verbose "Executing script: $($script.FullName)"
. $script.FullName # Call the script
}
} else {
Write-Error "No Private directory found for the latest version: $($latestVersionDir.Name). Exiting."
exit 1
}
# Check if the Private directory exists in the latest version and import scripts if it does
if (Test-Path -Path $kubePrivateDir) {
Write-Verbose "Executing scripts from KubeTidy Private directory for version: $($latestVersionDir.Name)"
Import-Scripts -directory $kubePrivateDir
} else {
Write-Error "No version directories found in $krewStorageDir. Exiting."
Write-Error "No Private directory found for the latest version: $($latestVersionDir.Name). Exiting."
exit 1
}
} else {
Write-Error "Krew storage directory for KubeTidy not found. Exiting."
Write-Error "No version directories found in $krewStorageDir. Exiting."
exit 1
}
} else {
Write-Error "Krew storage directory for KubeTidy not found. Exiting."
exit 1
}


Expand Down Expand Up @@ -137,6 +139,8 @@ function Invoke-KubeTidy {
.PARAMETER Verbose
Enables verbose logging for detailed output.
#>

Show-KubeTidyBanner

# If ExclusionList is not provided, set it to an empty array
if (-not $ExclusionList) {
Expand Down
2 changes: 1 addition & 1 deletion Private/Get-KubeConfigPath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Get-KubeConfigPath {
$KubeConfigPath = "$HOME/.kube/config"
}

Write-Host "KubeConfig Path: $KubeConfigPath" -ForegroundColor Yellow
Write-Host "KubeConfig Path: $KubeConfigPath `n" -ForegroundColor Yellow
}

# Return the KubeConfigPath
Expand Down
File renamed without changes.

0 comments on commit 425477d

Please sign in to comment.