ps/Modules/Alkami.PowerShell.Choco/Public/Get-PackageFileListV2.ps1
2023-05-30 22:51:22 -07:00

93 lines
3.5 KiB
PowerShell

function Get-PackageFileListV2 {
<#
.SYNOPSIS
Downloads a specific file from proget and returns it.
.PARAMETER FeedSource
[string] Source feed used to look up the package by
.PARAMETER Name
[string] Package name to lookup
.PARAMETER Version
[string] Package version to lookup
.PARAMETER PackagePath
Ignored. Provided for contract simplicity
.PARAMETER Credential
[PSCredential] Credential used for talking to feeds as needed
#>
[CmdletBinding(DefaultParameterSetName = 'RawArgs')]
Param(
[Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')]
[string]$FeedSource,
[Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')]
[string]$Name,
[Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')]
[string]$Version,
[Parameter(Mandatory = $true, ParameterSetName = 'Package')]
[object]$Package,
[Parameter(Mandatory = $false)]
[string]$PackagePath,
[Parameter(Mandatory = $false)]
[PSCredential]$Credential = $null
)
## TODO: Can this pull from the local filesystem if it exists?
## This would let us fetch faster if the versions match as we could avoid network hops.
$loglead = (Get-LogLeadName)
if ($PSCmdlet.ParameterSetName -eq 'Package') {
$FeedSource = $Package.Feed.Source
$Name = $Package.Name
$Version = $Package.Version
}
# Replace package path back slashes with forward slashes, to play nicely in the URL.
$packagePath = $packagePath.Replace("\", "/")
# Make sure the input feed URL is a nuget feed.
$feedUri = $null
try {
$feedUri = [System.Uri]::new($FeedSource)
if ($feedUri.Segments.Count -lt 3) {
throw "$logLead : FeedSource [$FeedSource] parameter for package [$Name] should have at least three url segments (host, nuget, feed name) and does not have enough feed segments provided."
}
$segmentCompare = 'nuget/'
if ($feedUri.Segments[1] -ne $segmentCompare) {
throw "$logLead : FeedSource [$FeedSource] parameter for package [$Name] should use the url segment [$segmentCompare], not [$($feedUri.Segments[1])]."
}
} catch {
Write-Error "$logLead : FeedSource [$FeedSource] parameter for package [$Name] was supplied incorrectly. Expected url pattern should look like: [https://feed.com/nuget/feed.name]"
Write-Error $_.Exception.Message
return
}
# we now have a valid feedUri, it has the right segment count, and we can infer otherwise as needed
$feedName = $feedUri.Segments[2].TrimEnd('/')
$baseUrl = $feedUri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped)
$filesUrl = "$baseUrl/package-files/list?packageId=$Name&version=$Version&feedName=$feedName"
# If a PSCredential is specified build a basic authentication header.
$headers = (Get-BasicAuthHeader -Credential $Credential)
# Query for all the files in the package.
Write-Host "$loglead : Querying for package files at endpoint: $filesUrl"
$response = Invoke-ProgetRequest -URI $filesUrl -Headers $headers
Write-Host "$loglead : Done Querying for package files at endpoint: $filesUrl"
try {
$jsonResponse = (ConvertFrom-Json -InputObject $response)
} catch {
Write-Error "Did not recieve json from proget request"
}
# Consider this a best practice, per gwhiting
$inlineFilterVariable = $jsonResponse.Where({!$_.fullPath.StartsWith("src/")})
return $inlineFilterVariable
}