functions/public/Get-KlippyConfigFile.ps1
|
function Get-KlippyConfigFile { <# .SYNOPSIS Gets configuration files from a Klipper printer. .DESCRIPTION Lists configuration files from the printer's config directory. Includes printer.cfg, moonraker.conf, and other config files. Supports wildcard filtering. .PARAMETER Id The unique identifier of the printer. .PARAMETER PrinterName The friendly name of the printer. .PARAMETER InputObject A printer object from pipeline input. .PARAMETER Path Path filter or pattern. Supports wildcards. .EXAMPLE Get-KlippyConfigFile Lists all config files on the default printer. .EXAMPLE Get-KlippyConfigFile -PrinterName "voronv2" -Path "*.cfg" Lists all .cfg files. .EXAMPLE Get-KlippyConfigFile -Path "printer*" Lists files matching "printer*". .OUTPUTS PSCustomObject[] with config file information. #> [CmdletBinding(DefaultParameterSetName = 'Default')] [OutputType([PSCustomObject[]])] param( [Parameter(Mandatory = $true, ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(Mandatory = $true, ParameterSetName = 'ByName')] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject, [Parameter(Position = 0)] [string]$Path ) process { # Resolve printer $resolveParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ById' { $resolveParams['Id'] = $Id } 'ByName' { $resolveParams['PrinterName'] = $PrinterName } 'ByObject' { $resolveParams['InputObject'] = $InputObject } } $printer = Resolve-KlippyPrinterTarget @resolveParams try { Write-Verbose "[$($printer.PrinterName)] Fetching config file list..." # Get file listing from config root $response = Invoke-KlippyHttpRequest -Printer $printer -Endpoint "server/files/list" -QueryParameters @{ root = "config" } # Filter to files only $fileList = $response | Where-Object { -not $_.Dirname } # Apply path filter if specified if ($Path) { $fileList = $fileList | Filter-KlippyItemByWildcard -Pattern $Path } # Transform to output objects $results = foreach ($file in $fileList) { [PSCustomObject]@{ PSTypeName = 'KlippyCLI.ConfigFile' PrinterId = $printer.Id PrinterName = $printer.PrinterName Path = $file.Path Name = Split-Path $file.Path -Leaf Size = $file.Size SizeKB = [Math]::Round($file.Size / 1KB, 2) Modified = if ($file.Modified) { [DateTimeOffset]::FromUnixTimeSeconds([long]$file.Modified).LocalDateTime } else { $null } } } return $results } catch { Write-Error "[$($printer.PrinterName)] Failed to list config files: $_" } } } |