Skip to content

Commit

Permalink
fix: ModuleIndex - Added CmdletBinding + moved code to simpify debugg…
Browse files Browse the repository at this point in the history
…ing (#1786)

## Description

- Added CmdletBinding 
- Moved code to simpify debugging
  • Loading branch information
AlexanderSehr authored Apr 30, 2024
1 parent 8f0e510 commit d4d7ddb
Showing 1 changed file with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The function requires Azure PowerShell Storage Module (Az.Storage) to be install
#>

function Invoke-AvmJsonModuleIndexGeneration {

[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string] $storageAccountName = 'biceplivedatasaprod',
Expand All @@ -63,31 +65,6 @@ function Invoke-AvmJsonModuleIndexGeneration {
[switch] $doNotMergeWithLastModuleIndexJsonFileVersion
)

## Download the current published moduleIndex.json from the storage account if the $doNotMergeWithLastModuleIndexJsonFileVersion is set to $false
if (-not $doNotMergeWithLastModuleIndexJsonFileVersion) {
try {
$lastModuleIndexJsonFilePath = $prefixForLastModuleIndexJsonFile + $moduleIndexJsonFilePath

Write-Verbose "Attempting to get last version of the moduleIndex.json from the Storage Account: $storageAccountName, Container: $storageAccountContainer, Blob: $storageBlobName and save to file: $lastModuleIndexJsonFilePath ..." -Verbose

$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount

Get-AzStorageBlobContent -Blob $storageBlobName -Container $storageAccountContainer -Context $storageContext -Destination $lastModuleIndexJsonFilePath -Force | Out-Null
} catch {
Write-Error "Unable to retrieve moduleIndex.json file from the Storage Account: $storageAccountName, Container: $storageAccountContainer, Blob: $storageBlobName. Error: $($_.Exception.Message)" -ErrorAction Stop
}

## Check if the last version of the moduleIndex.json (last-moduleIndex.json) file exists and is not empty

if (Test-Path $lastModuleIndexJsonFilePath) {
$lastModuleIndexJsonFileContent = Get-Content $lastModuleIndexJsonFilePath
if ($null -eq $lastModuleIndexJsonFileContent) {
Write-Error "The last version of the moduleIndex.json file (last-moduleIndex.json) exists but is empty. File: $lastModuleIndexJsonFilePath" -ErrorAction Stop
}
Write-Verbose 'The last version of the moduleIndex.json file (last-moduleIndex.json) exists and is not empty. Proceeding...' -Verbose
}
}

## Generate the new moduleIndex.json file based off the modules in the repository

$currentGeneratedModuleIndexJsonFilePath = $prefixForCurrentGeneratedModuleIndexJsonFile + $moduleIndexJsonFilePath
Expand Down Expand Up @@ -167,9 +144,31 @@ function Invoke-AvmJsonModuleIndexGeneration {
Write-Verbose "Convert moduleIndexData variable to JSON and save as 'generated-moduleIndex.json'" -Verbose
$moduleIndexData | ConvertTo-Json -Depth 10 | Out-File -FilePath $currentGeneratedModuleIndexJsonFilePath

## Merge the new moduleIndex.json file with the previous version if the $doNotMergeWithLastModuleIndexJsonFileVersion is not specified

## Download the current published moduleIndex.json from the storage account if the $doNotMergeWithLastModuleIndexJsonFileVersion is set to $false
if (-not $doNotMergeWithLastModuleIndexJsonFileVersion) {
try {
$lastModuleIndexJsonFilePath = $prefixForLastModuleIndexJsonFile + $moduleIndexJsonFilePath

Write-Verbose "Attempting to get last version of the moduleIndex.json from the Storage Account: $storageAccountName, Container: $storageAccountContainer, Blob: $storageBlobName and save to file: $lastModuleIndexJsonFilePath ..." -Verbose

$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount

Get-AzStorageBlobContent -Blob $storageBlobName -Container $storageAccountContainer -Context $storageContext -Destination $lastModuleIndexJsonFilePath -Force | Out-Null
} catch {
Write-Error "Unable to retrieve moduleIndex.json file from the Storage Account: $storageAccountName, Container: $storageAccountContainer, Blob: $storageBlobName. Error: $($_.Exception.Message)" -ErrorAction 'Stop'
}

## Check if the last version of the moduleIndex.json (last-moduleIndex.json) file exists and is not empty

if (Test-Path $lastModuleIndexJsonFilePath) {
$lastModuleIndexJsonFileContent = Get-Content $lastModuleIndexJsonFilePath
if ($null -eq $lastModuleIndexJsonFileContent) {
Write-Error "The last version of the moduleIndex.json file (last-moduleIndex.json) exists but is empty. File: $lastModuleIndexJsonFilePath" -ErrorAction 'Stop'
}
Write-Verbose 'The last version of the moduleIndex.json file (last-moduleIndex.json) exists and is not empty. Proceeding...' -Verbose
}

## Merge the new moduleIndex.json file with the previous version if the $doNotMergeWithLastModuleIndexJsonFileVersion is not specified
Write-Verbose "Merging 'generated-moduleIndex.json' (new) file with 'last-moduleIndex.json' (previous) file..." -Verbose

$lastModuleIndexJsonFileContent = Get-Content $lastModuleIndexJsonFilePath
Expand Down

0 comments on commit d4d7ddb

Please sign in to comment.