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

60 lines
2.0 KiB
PowerShell

function Get-ChocolateySources {
<#
.SYNOPSIS
Returns a list of locally configured chocolatey feed objects.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$hostname = "localhost",
[Parameter(Mandatory = $false)]
[Alias("IncludeDisabled")]
[switch]$includeDisabledSources
)
$logLead = (Get-logLeadName)
# Grab chocolatey feeds configured on the local machine or server.
$sources = $null;
if(($hostname -eq "localhost") -or ($hostname -eq "."))
{
$sources = (choco source list -r);
}
else
{
$sources = Invoke-Command -ComputerName $hostname -ScriptBlock { return (choco source list -r) };
}
$alkamiFeedMatch = "https://packagerepo.orb.alkamitech.com/nuget/choco.*";
$sdkFeedMatch = "https://feeds.alkamitech.com/nuget/*";
$resultSources = @();
foreach ($sourceToCheck in $sources) {
$splitSource = $sourceToCheck.Split("|")
if (!($includeDisabledSources) -and ($splitSource[2] -eq "True")) {
Write-Host "$logLead : Skipping $sourceToCheck"
continue;
}
$isDefaultFeed = $splitSource[0] -eq "chocolatey";
$properties = @{
Name = $splitSource[0];
Source = $splitSource[1];
Priority = $splitSource[5];
Disabled = if ($splitSource[2] -like "true") { $true; } else { $false; }
IsDefault = $isDefaultFeed;
IsSDK = (!$isDefaultFeed) -and ($splitSource[1] -like $sdkFeedMatch);
};
$source = New-Object -TypeName PSObject -Prop $properties;
$resultSources += $source;
}
# Re-arrange the choco sources to prioritize the Alkami choco.* feeds to the top, and SDK feeds second. It speeds up searching!
$resultSources = ($resultSources | Sort-Object -Property @{Expression={ [int]($_.Source -like $alkamiFeedMatch) * 2 + [int]($_.Source -like $sdkFeedMatch)}} -Descending);
return $resultSources;
}