Functions/Get-Settings.ps1

Function Get-Settings {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification='Not changed yet')]
    
    param(
        [parameter(Mandatory=$true, Position=0)]
        [string]$configRootPath,
        [string]$environment,
        [string]$SettingDelimiter = '.',
        [parameter(ValueFromRemainingArguments = $true)]
        $overrides
    )
 
    $basePath = join-path $configRootPath (GetBaseConfigPath) 
    
    $basePath = GetFirstConfigFile $basePath

    $settings = [PSCustomObject]@{}
    Import-SettingsFromFile $settings $basePath 

    if ($environment){
        $envPath = join-path $configRootPath (GetEnvironmentConfigPath -environment $environment)

        $envPath = GetFirstConfigFile $envPath
        Write-Verbose "Loading config from $environment"    
        Import-SettingsFromFile $settings $envPath 
    }
    
    Import-SettingsFromPersonalFile -ConfigRootPath $configRootPath -Settings $settings -environment $environment
    
    #Loop through environment variables and set values
    Get-ChildItem env: -PipelineVariable envSetting| ForEach-Object{
        Write-Verbose "Setting $($_.Name) to $($envSetting.value) from an environment parameter passed in"
        Set-SettingsValue -Settings $settings -Name $envSetting.name -Value $envSetting.value -SettingDelimiter $SettingDelimiter
    }
    
    if ($settings.keyVaultConfigs) {
        $settings.keyVaultConfigs | Foreach-Object {
            $Decrypt = $false
            if ( $_ -is [string]){
                $keyVault = $_
            }
            else{
                $keyVault = $_.Name
                $Decrypt = $_.Decrypt
            }
        
            (Get-KeyVaultSecrets -vaultName $keyVault) | ForEach-Object {    
                $property = $_
                $value = if ($Decrypt){GetSecret $property.Value}else{$property.Value}
                Write-Verbose "Adding $($property.Name) -Value $($property.Value) from keyvault: $keyVault"
                Set-SettingsValue $settings -Name $property.Name -Value $value  -SettingDelimiter  $SettingDelimiter
    
            }
        }
    }

    if (-not $null -eq $overrides) {
        Write-Host "Loading settings from overrides"

        #Overrides is an array of the remaining arguments passed through.
        #if the following had been passed -param1 value -param2 value2
        #then you would have a 4 item array i.e. @("-param1","value","-param2","value2")

        for ($i = 0; $i -lt $overrides.Count; $i++) {
            $name = $overrides[$i] -replace  "-?([^:]*):?",'$1'
            #Switch parameters are either the last param or the next item is another param and starts with a -
            if ($i -eq $overrides.count - 1 -or $overrides[$i + 1][0] -eq "-") {
                $value = $true
            }
            else {
                $value = $overrides[$i + 1]
                $i++
            }
            Write-Verbose "Setting $name to $value from an override parameter passed in"
            Set-SettingsValue -Settings $settings -Name $name -Value $value  -SettingDelimiter $SettingDelimiter  -ThrowIfNotFound
        }
    }


    $settings = Invoke-SettingEvaluation -settings $settings
    $settings
}