mirror of
https://github.com/ScoopInstaller/Scoop.git
synced 2025-12-12 11:06:34 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 <your 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 <name>')"
|
||||
""
|
||||
Write-Host "Results from other known buckets...
|
||||
(add them using 'scoop bucket add <bucket name>')"
|
||||
}
|
||||
|
||||
$results | ForEach-Object {
|
||||
"'$($_.bucket)' bucket (install using 'scoop install $($_.bucket)/<app>'):"
|
||||
$_.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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user