From 847756bd1e441979c121da8b20b67b56bc53814c Mon Sep 17 00:00:00 2001 From: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> Date: Tue, 21 Jun 2022 11:48:58 +0530 Subject: [PATCH] refactor(scoop-search): Output PSObject, use API token (#4997) * refactor(scoop-search): Output PSObject, use API token * warn about parsing error * Update CHANGELOG.md * Update scoop-search.ps1 * Apply suggestions from code review Co-authored-by: Hsiao-nan Cheung * separate lines Co-authored-by: Hsiao-nan Cheung --- CHANGELOG.md | 4 ++ lib/manifest.ps1 | 8 ++- libexec/scoop-search.ps1 | 106 +++++++++++++++++++++++++-------------- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84ced0ebb..8b0572fc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - **shortcuts:** Fix network drive shortcut creation ([#4410](https://github.com/ScoopInstaller/Scoop/issues/4410)) +### Code Refactoring + +- **scoop-search:** Output PSObject, use API token ([#4997](https://github.com/ScoopInstaller/Scoop/issues/4997)) + ## [v0.2.2](https://github.com/ScoopInstaller/Scoop/compare/v0.2.1...v0.2.2) - 2022-06-21 ### Features diff --git a/lib/manifest.ps1 b/lib/manifest.ps1 index ffa76c750..ebd99f9ab 100644 --- a/lib/manifest.ps1 +++ b/lib/manifest.ps1 @@ -3,8 +3,12 @@ function manifest_path($app, $bucket) { } function parse_json($path) { - if(!(test-path $path)) { return $null } - Get-Content $path -raw -Encoding UTF8 | convertfrom-json -ea stop + if (!(Test-Path $path)) { return $null } + try { + Get-Content $path -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop + } catch { + warn "Error parsing manifest at $path." + } } function url_manifest($url) { diff --git a/libexec/scoop-search.ps1 b/libexec/scoop-search.ps1 index d878e5330..adef8b183 100644 --- a/libexec/scoop-search.ps1 +++ b/libexec/scoop-search.ps1 @@ -9,35 +9,43 @@ param($query) . "$PSScriptRoot\..\lib\manifest.ps1" # 'manifest' . "$PSScriptRoot\..\lib\versions.ps1" # 'Get-LatestVersion' -function bin_match($manifest, $query) { - if(!$manifest.bin) { return $false } - foreach($bin in $manifest.bin) { - $exe, $alias, $args = $bin - $fname = split-path $exe -leaf -ea stop +$list = @() - if((strip_ext $fname) -match $query) { return $fname } - if($alias -match $query) { return $alias } +try { + $query = New-Object Regex $query, 'IgnoreCase' +} catch { + abort "Invalid regular expression: $($_.Exception.InnerException.Message)" +} + +$githubtoken = Get-GitHubToken +$authheader = @{} +if ($githubtoken) { + $authheader = @{'Authorization' = "token $githubtoken"} +} + +function bin_match($manifest, $query) { + if (!$manifest.bin) { return $false } + $bins = foreach ($bin in $manifest.bin) { + $exe, $alias, $args = $bin + $fname = Split-Path $exe -Leaf -ErrorAction Stop + + if ((strip_ext $fname) -match $query) { $fname } + elseif ($alias -match $query) { $alias } } - $false + if ($bins) { return $bins } + else { return $false } } function search_bucket($bucket, $query) { - $apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object { - @{ name = $_ } - } - - if($query) { - try { - $query = new-object regex $query, 'IgnoreCase' - } catch { - abort "Invalid regular expression: $($_.exception.innerexception.message)" - } + $apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object { @{ name = $_ } } + if ($query) { $apps = $apps | Where-Object { - if($_.name -match $query) { return $true } + if ($_.name -match $query) { return $true } $bin = bin_match (manifest $_.name $bucket) $query - if($bin) { - $_.bin = $bin; return $true; + if ($bin) { + $_.bin = $bin + return $true } } } @@ -46,14 +54,19 @@ function search_bucket($bucket, $query) { function download_json($url) { $ProgressPreference = 'SilentlyContinue' - $result = Invoke-WebRequest $url -UseBasicParsing | Select-Object -ExpandProperty content | ConvertFrom-Json + $result = Invoke-WebRequest $url -UseBasicParsing -Headers $authheader | Select-Object -ExpandProperty content | ConvertFrom-Json $ProgressPreference = 'Continue' $result } function github_ratelimit_reached { $api_link = 'https://api.github.com/rate_limit' - (download_json $api_link).rate.remaining -eq 0 + $ret = (download_json $api_link).rate.remaining -eq 0 + if ($ret) { + Write-Host "GitHub API rate limit reached. +Please try again later or configure your API token using 'scoop config gh_token '." + } + $ret } function search_remote($bucket, $query) { @@ -72,44 +85,59 @@ function search_remote($bucket, $query) { function search_remotes($query) { $buckets = known_bucket_repos - $names = $buckets | get-member -m noteproperty | Select-Object -exp name + $names = $buckets | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name - $results = $names | Where-Object { !(test-path $(Find-BucketDirectory $_)) } | ForEach-Object { - @{"bucket" = $_; "results" = (search_remote $_ $query)} + $results = $names | Where-Object { !(Test-Path $(Find-BucketDirectory $_)) } | ForEach-Object { + @{ "bucket" = $_; "results" = (search_remote $_ $query) } } | Where-Object { $_.results } if ($results.count -gt 0) { - "Results from other known buckets..." - "(add them using 'scoop bucket add ')" - "" + Write-Host "Results from other known buckets... +(add them using 'scoop bucket add ')" } $results | ForEach-Object { - "'$($_.bucket)' bucket (install using 'scoop install $($_.bucket)/'):" - $_.results | ForEach-Object { " $_" } - "" + $name = $_.bucket + $_.results | ForEach-Object { + $item = [ordered]@{} + $item.Name = $_ + $item.Source = $name + $list += [PSCustomObject]$item + } } + + $list } Get-LocalBucket | ForEach-Object { $res = search_bucket $_ $query $local_results = $local_results -or $res - if($res) { + if ($res) { $name = "$_" - Write-Host "'$name' bucket:" $res | ForEach-Object { - $item = " $($_.name) ($($_.version))" - if($_.bin) { $item += " --> includes '$($_.bin)'" } - $item + $item = [ordered]@{} + $item.Name = $_.name + $item.Version = $_.version + $item.Source = $name + $item.Binaries = "" + if ($_.bin) { $item.Binaries = $_.bin -join ' | ' } + $list += [PSCustomObject]$item } - "" } } +if ($list.Length -gt 0) { + Write-Host "Results from local buckets..." + $list +} + if (!$local_results -and !(github_ratelimit_reached)) { $remote_results = search_remotes $query - if(!$remote_results) { [console]::error.writeline("No matches found."); exit 1 } + if (!$remote_results) { + warn "No matches found." + exit 1 + } $remote_results }