Public/New-GitHubCacheRestoreKey.ps1

function New-GitHubCacheRestoreKey {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [OutputType([string])]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string[]] $Segment,

        [ValidateRange(1, [int]::MaxValue)]
        [int] $MinimumSegmentCount = 1,

        [ValidateNotNullOrEmpty()]
        [string] $Separator = '-',

        [switch] $IncludePrimaryKey
    )

    $normalizedSegments = foreach ($currentSegment in $Segment) {
        $normalizedSegment = ConvertTo-GitHubCacheSegmentInternal -Value $currentSegment
        if ($null -ne $normalizedSegment) {
            $normalizedSegment
        }
    }

    if (@($normalizedSegments).Count -lt $MinimumSegmentCount) {
        throw 'MinimumSegmentCount cannot be greater than the number of cache key segments.'
    }

    if ((@($normalizedSegments).Count -lt 2) -and -not $IncludePrimaryKey) {
        throw 'At least two cache key segments are required to generate restore keys unless IncludePrimaryKey is specified.'
    }

    $restoreKeys = [System.Collections.Generic.List[string]]::new()
    if ($IncludePrimaryKey) {
        $restoreKeys.Add((New-GitHubCacheKeyTextInternal -Segment $normalizedSegments -Separator $Separator))
    }

    for ($segmentCount = @($normalizedSegments).Count - 1; $segmentCount -ge $MinimumSegmentCount; $segmentCount--) {
        $prefix = New-GitHubCacheKeyTextInternal -Segment $normalizedSegments[0..($segmentCount - 1)] -Separator $Separator
        $restoreKeys.Add(('{0}{1}' -f $prefix, $Separator))
    }

    if ($restoreKeys.Count -eq 0) {
        throw 'No restore keys could be generated from the supplied cache key segments.'
    }

    return $restoreKeys -join [System.Environment]::NewLine
}