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 <niheaven@gmail.com>

* separate lines

Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
This commit is contained in:
Rashil Gandhi
2022-06-21 11:48:58 +05:30
committed by GitHub
parent 86e3efbaf0
commit 847756bd1e
3 changed files with 77 additions and 41 deletions

View File

@@ -4,6 +4,10 @@
- **shortcuts:** Fix network drive shortcut creation ([#4410](https://github.com/ScoopInstaller/Scoop/issues/4410)) - **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 ## [v0.2.2](https://github.com/ScoopInstaller/Scoop/compare/v0.2.1...v0.2.2) - 2022-06-21
### Features ### Features

View File

@@ -3,8 +3,12 @@ function manifest_path($app, $bucket) {
} }
function parse_json($path) { function parse_json($path) {
if(!(test-path $path)) { return $null } if (!(Test-Path $path)) { return $null }
Get-Content $path -raw -Encoding UTF8 | convertfrom-json -ea stop try {
Get-Content $path -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop
} catch {
warn "Error parsing manifest at $path."
}
} }
function url_manifest($url) { function url_manifest($url) {

View File

@@ -9,35 +9,43 @@ param($query)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'manifest' . "$PSScriptRoot\..\lib\manifest.ps1" # 'manifest'
. "$PSScriptRoot\..\lib\versions.ps1" # 'Get-LatestVersion' . "$PSScriptRoot\..\lib\versions.ps1" # 'Get-LatestVersion'
function bin_match($manifest, $query) { $list = @()
if(!$manifest.bin) { return $false }
foreach($bin in $manifest.bin) {
$exe, $alias, $args = $bin
$fname = split-path $exe -leaf -ea stop
if((strip_ext $fname) -match $query) { return $fname } try {
if($alias -match $query) { return $alias } $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) { function search_bucket($bucket, $query) {
$apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object { $apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object { @{ name = $_ } }
@{ name = $_ }
}
if($query) {
try {
$query = new-object regex $query, 'IgnoreCase'
} catch {
abort "Invalid regular expression: $($_.exception.innerexception.message)"
}
if ($query) {
$apps = $apps | Where-Object { $apps = $apps | Where-Object {
if($_.name -match $query) { return $true } if ($_.name -match $query) { return $true }
$bin = bin_match (manifest $_.name $bucket) $query $bin = bin_match (manifest $_.name $bucket) $query
if($bin) { if ($bin) {
$_.bin = $bin; return $true; $_.bin = $bin
return $true
} }
} }
} }
@@ -46,14 +54,19 @@ function search_bucket($bucket, $query) {
function download_json($url) { function download_json($url) {
$ProgressPreference = 'SilentlyContinue' $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' $ProgressPreference = 'Continue'
$result $result
} }
function github_ratelimit_reached { function github_ratelimit_reached {
$api_link = 'https://api.github.com/rate_limit' $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 <your token>'."
}
$ret
} }
function search_remote($bucket, $query) { function search_remote($bucket, $query) {
@@ -72,44 +85,59 @@ function search_remote($bucket, $query) {
function search_remotes($query) { function search_remotes($query) {
$buckets = known_bucket_repos $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 { $results = $names | Where-Object { !(Test-Path $(Find-BucketDirectory $_)) } | ForEach-Object {
@{"bucket" = $_; "results" = (search_remote $_ $query)} @{ "bucket" = $_; "results" = (search_remote $_ $query) }
} | Where-Object { $_.results } } | Where-Object { $_.results }
if ($results.count -gt 0) { if ($results.count -gt 0) {
"Results from other known buckets..." Write-Host "Results from other known buckets...
"(add them using 'scoop bucket add <name>')" (add them using 'scoop bucket add <bucket name>')"
""
} }
$results | ForEach-Object { $results | ForEach-Object {
"'$($_.bucket)' bucket (install using 'scoop install $($_.bucket)/<app>'):" $name = $_.bucket
$_.results | ForEach-Object { " $_" } $_.results | ForEach-Object {
"" $item = [ordered]@{}
$item.Name = $_
$item.Source = $name
$list += [PSCustomObject]$item
}
} }
$list
} }
Get-LocalBucket | ForEach-Object { Get-LocalBucket | ForEach-Object {
$res = search_bucket $_ $query $res = search_bucket $_ $query
$local_results = $local_results -or $res $local_results = $local_results -or $res
if($res) { if ($res) {
$name = "$_" $name = "$_"
Write-Host "'$name' bucket:"
$res | ForEach-Object { $res | ForEach-Object {
$item = " $($_.name) ($($_.version))" $item = [ordered]@{}
if($_.bin) { $item += " --> includes '$($_.bin)'" } $item.Name = $_.name
$item $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)) { if (!$local_results -and !(github_ratelimit_reached)) {
$remote_results = search_remotes $query $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 $remote_results
} }