diff --git a/CHANGELOG.md b/CHANGELOG.md index f865df73a9..ce053133dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [0.6.5](https://github.com/Ash258/Scoop-Core/milestone/5) +- **scoop-search**: Add `-a`, `--api` paramters - **Tests**: Fix detection of 1 new line at the end and allow CRLF/LF for powershell executables - **MacOS**: Properly detect arm architecture - Support installation of archives under *nix diff --git a/lib/Search.ps1 b/lib/Search.ps1 index 82dc312ac8..1802cdef3b 100644 --- a/lib/Search.ps1 +++ b/lib/Search.ps1 @@ -226,4 +226,53 @@ function Search-LocalBucket { end { return $result } } +function Search-RemoteAPI { + [CmdletBinding()] + param([String] $Query = '%') + + process { + if ([String]::IsNullOrEmpty($Query)) { $Query = '%' } + + $api = get_config 'shovelSearchAPI' 'https://api.shovel.ash258.com/api/v1' + $res = @{} + $buckets = @() + try { + # TODO: Pagination just for the convenience + $buckets = Invoke-RestMethod -Uri "$api/bucket?size=10000&page=0" + } catch { + throw "Cannot get buckets information from API: $($_.Exception.Message)" + } + + $final = @() + $page = $total = 0 + + do { + try { + $res = Invoke-RestMethod -Uri "$api/manifest/search?query=$Query&size=1000&page=$page" + } catch { + throw "Cannot get manifests from API: $($_.Exception.Message)" + } + ++$page + $total = $res.total + $final += $res.content + } while ($total -ne $final.Count) + + # TODO: test if bucket is added + # TODO: test if bucket is known + # TODO: Remove deprecated + # TODO: Process results + # $apps += @{ + # 'name' = $resolved.ApplicationName + # 'version' = $manifest.version + # 'description' = $manifest.description + # 'bin' = @(arch_specific 'bin' $manifest $architecture) + # 'matchingBinaries' = @() + # 'shortcuts' = @(arch_specific 'shortcuts' $manifest $architecture) + # 'matchingShortcuts' = @() + # } + + return $final + } +} + $__importedSearch__ = $true diff --git a/libexec/scoop-config.ps1 b/libexec/scoop-config.ps1 index d621fe355c..13ecb853f0 100644 --- a/libexec/scoop-config.ps1 +++ b/libexec/scoop-config.ps1 @@ -90,6 +90,10 @@ # dbgBypassArmCheck: $true|$false # Do not fail to install arm64 version on x86 platform. # +# shovelSearchAPI: +# Provide alternative search API for Shovel. Mainly for debugging. +# Has to be full API URL. Including protocol, API version. +# # ARIA2 configuration # ------------------- # diff --git a/libexec/scoop-search.ps1 b/libexec/scoop-search.ps1 index b2c22113d4..def2b0411e 100644 --- a/libexec/scoop-search.ps1 +++ b/libexec/scoop-search.ps1 @@ -8,6 +8,9 @@ # -r, --remote Force remote search in known buckets using Github API. # Remote search does not utilize advanced search methods (descriptions, binary, shortcuts, ... matching). # It only uses manifest name to search. +# -a, --api Use shovel.ash258.com API backend for search. +# Regular expression is not supported. +# Currently in experimental phase with limited feature-set. Search is done on manifest names only. 'core', 'getopt', 'help', 'Helpers', 'buckets', 'Search' | ForEach-Object { . (Join-Path $PSScriptRoot "..\lib\${_}.ps1") @@ -15,11 +18,32 @@ $ExitCode = 0 $LocalResults = @() -$Options, $Query, $_err = Resolve-GetOpt $args 'r' 'remote' +$Options, $Query, $_err = Resolve-GetOpt $args 'ra' 'remote', 'api' if ($_err) { Stop-ScoopExecution -Message "scoop search: $_err" -ExitCode 2 } +$Query = $Query[0] $Remote = $Options.r -or $Options.remote +$Api = $Options.a -or $Options.api + +if ($Api) { + try { + $results = Search-RemoteAPI -Query $Query + } catch { + Stop-ScoopExecution -Message "scoop search: $_" -ExitCode 3 + } + + # TODO: Bucket url + # TODO: Prompt for bucket addition + if ($results.Count -eq 0) { + Write-Host "No results found for query '$Query'" + } + + $results | ForEach-Object { + Write-Host "$($_.name)@$($_.version) - $($_.url)" -f Magenta + } +} +exit 0 if ($Query) { try { @@ -67,7 +91,7 @@ foreach ($bucket in (Get-LocalBucket)) { } if (!$LocalResults) { Write-UserMessage -Message 'No matches in local buckets found' } -if (!$LocalResults -or $Remote) { +if (!$Api -and (!$LocalResults -or $Remote)) { if (!(Test-GithubApiRateLimitBreached)) { Write-UserMessage -Message 'Searching in remote buckets ...' $remoteResults = Search-AllRemote -Query $Query @@ -86,4 +110,13 @@ if (!$LocalResults -or $Remote) { } } +if ($Api) { + $results = Search-RemoteAPI -Query $Query + + exit 258 + $results | ForEach-Object { + Write-Host "$($_.name)@$($_.version) - $($_.url)" -f red + } +} + exit $ExitCode diff --git a/supporting/completion/Scoop-Completion.psm1 b/supporting/completion/Scoop-Completion.psm1 index 9c8058a722..f12d5eb1e8 100644 --- a/supporting/completion/Scoop-Completion.psm1 +++ b/supporting/completion/Scoop-Completion.psm1 @@ -50,7 +50,7 @@ $script:SCOOP_SHORT_PARAMETERS = @{ 'info' = 'a' 'install' = 'g i k s a' 'list' = 'i u r' - 'search' = 'r' + 'search' = 'a r' 'unhold' = 'g' 'uninstall' = 'g p' 'update' = 'f g i k s q' @@ -66,7 +66,7 @@ $script:SCOOP_LONG_PARAMETERS = @{ 'info' = 'arch' 'install' = 'global independent no-cache skip arch' 'list' = 'installed updated reverse' - 'search' = 'remote' + 'search' = 'api remote' 'unhold' = 'global' 'uninstall' = 'global purge' 'update' = 'force global independent no-cache skip quiet' diff --git a/supporting/completion/scoop.lua b/supporting/completion/scoop.lua index f138deb94f..23aec2e9bd 100644 --- a/supporting/completion/scoop.lua +++ b/supporting/completion/scoop.lua @@ -39,6 +39,7 @@ local configOptions = parser({ 'virustotal_api_key', 'proxy', 'githubToken' + 'shovelSearchAPI' }) -- region Functions @@ -216,6 +217,7 @@ local scoopParser = parser({ ):loop(1), 'search' .. parser({ '-h', '--help', + '-a', '--api' '-r', '--remote' }), 'status' .. parser({'-h', '--help'}),