Helpers/GeneralHelper.psm1


function Get-ReplacedString
{
    [OutputType([String])]
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0
        )]
        [String]$String,

        [Parameter(
            Mandatory=$true,
            Position=1
        )]
        [Hashtable[]]$Placeholders
    )

    $Placeholders | ForEach-Object {
        $String = $String.Replace("{$($_.Name)}", "$($_.Value)")
    }
    return $String
}

function Test-StringValidYaml
{
    [OutputType([Boolean])]
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0
        )]
        [String]$String
    )

    try { return !($Null -eq ($String | ConvertFrom-Yaml)) }
    catch { return $False }
}

function Test-ObjectValidYaml
{
    [OutputType([Boolean])]
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0
        )]
        [Hashtable]$Object
    )

    try { return !($Null -eq (ConvertTo-Yaml $Object)) }
    catch { return $False }
}

function ConvertFrom-YamlFile
{
    [OutputType([Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0
        )]
        [ValidateScript({Test-Path $_})]
        [String]$Path
    )

    $YamlAsString = Get-Content $Path -Raw
    
    if (Test-StringValidYaml $YamlAsString) {
        return $YamlAsString | ConvertFrom-Yaml
    }
    else {
        Throw "Invalid Yaml Found at $($Path)."
    }
}