From 6168877e559a3f2c32198937fdfe7c19b355ec7b Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Sat, 3 Apr 2021 13:37:55 -0500 Subject: [PATCH] Fix Native API calls to Chocolatey.org --- CHANGELOG.md | 4 ++++ src/ChocolateyGet.psd1 | 2 +- src/private/Invoke-Choco.ps1 | 11 ++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abcdee0..4a83d36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.1.1] - 2021-04-03 +#### Fixed +* No longer throws an error when searching for or installing packages from Chocolatey.org using the native API + ## [2.1.0] - 2021-01-23 #### Changed * Change default search to use exact package name if both package name and required version are specified (#20) diff --git a/src/ChocolateyGet.psd1 b/src/ChocolateyGet.psd1 index c4ccd9f..ee631ba 100644 --- a/src/ChocolateyGet.psd1 +++ b/src/ChocolateyGet.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'ChocolateyGet.psm1' - ModuleVersion = '2.1.0' + ModuleVersion = '2.1.1' GUID = 'c1735ed7-8b2f-426a-8cbc-b7feb6b8288d' Author = 'Jianyun' Copyright = '' diff --git a/src/private/Invoke-Choco.ps1 b/src/private/Invoke-Choco.ps1 index 9a13c4a..565cdb8 100644 --- a/src/private/Invoke-Choco.ps1 +++ b/src/private/Invoke-Choco.ps1 @@ -127,12 +127,13 @@ function Invoke-Choco { Write-Debug ("Invoking the Choco API with the following configuration: $($ChocoAPI.GetConfiguration() | Out-String)") # This invocation looks gross, but PowerShell currently lacks a clean way to call the parameter-less .NET generic method that Chocolatey uses for returning data $ChocoAPI.GetType().GetMethod('List').MakeGenericMethod([chocolatey.infrastructure.results.PackageResult]).Invoke($ChocoAPI,$null) | ForEach-Object { - # If searching local packages, we need to spoof the source name returned by the API with a generic default - if ($LocalOnly) { + + # Attempt to translate the source URL back into a human-readable source name + $_.Source = $ChocoAPI.GetConfiguration().MachineSources | Where-Object Key -eq $_.Source | Select-Object -ExpandProperty Name + + # If searching local packages, or nothing got returned, we need to spoof the source name returned by the API with a generic default + if ((-not $_.Source) -or $LocalOnly) { $_.Source = $script:PackageSourceName - } else { - # Otherwise, convert the source URI returned by Choco to a source name - $_.Source = $ChocoAPI.GetConfiguration().MachineSources | Where-Object Key -eq $_.Source | Select-Object -ExpandProperty Name } $swid = @{