functions/Get-EndjinGists.ps1

# <copyright file="Get-EndjinGists.ps1" company="Endjin Limited">
# Copyright (c) Endjin Limited. All rights reserved.
# </copyright>

<#
.SYNOPSIS
    Lists all available gists from the gist registry.
 
.DESCRIPTION
    The Get-EndjinGists function reads the gist-map configuration file and returns
    information about all available gists. By default, it returns PSCustomObjects
    suitable for pipeline processing. Use the -Summary switch for a human-readable
    grouped text display.
 
.PARAMETER Summary
    When specified, outputs a grouped text summary instead of PSCustomObjects.
 
.PARAMETER GistMapPath
    The path to a 'Gist Map' configuration file. Defaults to the configuration file
    distributed with the module (gist-map.yml).
 
.EXAMPLE
    Get-EndjinGists
 
    Returns all available gists as PSCustomObjects with Group, Name, Description, Source, and Ref properties.
 
.EXAMPLE
    Get-EndjinGists -Summary
 
    Displays a grouped text summary of all available gists.
 
.EXAMPLE
    Get-EndjinGists | Where-Object { $_.Group -eq 'llm-kb' }
 
    Returns only gists in the 'llm-kb' group.
 
.NOTES
    The gist-map.yml file contains the registry of all available gists organized by group.
#>

function Get-EndjinGists {
    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param (
        [Parameter()]
        [switch] $Summary,

        [Parameter()]
        [string] $GistMapPath = (Join-Path $PSScriptRoot '..' 'gist-map.yml')
    )

    begin {
        Set-StrictMode -Version Latest
    }

    process {
        $gistMapContent = Get-Content -Path $GistMapPath -Raw
        $gistMap = ConvertFrom-Yaml $gistMapContent

        if ($Summary) {
            Write-Output "Available Gists:"
            Write-Output ""
            foreach ($group in $gistMap.Keys) {
                Write-Output " $group"
                foreach ($gist in $gistMap[$group]) {
                    $desc = if ($gist.ContainsKey('description')) { $gist.description } else { $null }
                    $descText = if ($desc) { " - $desc" } else { "" }
                    Write-Output " - $($gist.name)$descText"
                }
                Write-Output ""
            }
        }
        else {
            $gists = [System.Collections.Generic.List[PSCustomObject]]::new()
            foreach ($group in $gistMap.Keys) {
                foreach ($gist in $gistMap[$group]) {
                    $desc = if ($gist.ContainsKey('description')) { $gist.description } else { $null }
                    $gists.Add([PSCustomObject]@{
                        Group       = $group
                        Name        = $gist.name
                        Description = $desc
                        Source      = $gist.source
                        Ref         = $gist.ref
                    })
                }
            }
            return $gists
        }
    }
}