@[has_globals] module main import net.urllib import json2 pub struct PackagePkgbuild { pub mut: depends []string @[json: Depends] make_depends []string @[json: MakeDepends] check_depends []string @[json: CheckDepends] description string @[json: Description] first_submitted i64 @[json: FirstSubmitted] id i64 @[json: ID] last_modified i64 @[json: LastModified] maintainer ?string @[json: Maintainer] name string @[json: Name] num_votes i64 @[json: NumVotes] out_of_date ?i64 @[json: OutOfDate] package_base string @[json: PackageBase] package_base_id i64 @[json: PackageBaseID] popularity f64 @[json: Popularity] url string @[json: URL] url_path string @[json: URLPath] version string @[json: Version] } pub struct PackagesPkgbuild { pub mut: count i64 @[json: resultcount] results []PackagePkgbuild type string version i64 // the rpc's own failure, when it refuses the request; absent when it answers error_text ?string @[json: error] } // pkgbuild_rpc_timeout is how long one rpc request may take, for the search // and the info lookups of the pkgbuild repos. a stall would otherwise hang // the search under a proxy's silent wall // (pkgbuild #:timeout) sets it, in seconds __global pkgbuild_rpc_timeout = 10 // pkgbuild_rpc fetches one url and reads its answer: timed, and demanding an // http answer the rpc actually gives. what it fetches is returned as text; // the callers decode their own answer out of it fn pkgbuild_rpc(url string, what string) !string { return http_text(url, pkgbuild_rpc_timeout, what, 'the pkgbuild rpc') } // answer_start is the start of an rpc answer that did not decode, for the // error: at most 200 bytes, of an answer that may be shorter (a captive // portal's page, a proxy's error) — a plain slice would panic on it fn answer_start(body string) string { return body#[..200] } // get_pkgbuild_packages searches one pkgbuild repo's site for a needle. // the needle is escaped: a query with '&', '#', ' ' or '=' must not break // the request or smuggle in extra rpc parameters fn get_pkgbuild_packages(base string, needle string) ![]PackagePkgbuild { body := pkgbuild_rpc( '${base}/rpc/v5/search/${urllib.query_escape(needle)}', 'search', )! data := json2.decode[PackagesPkgbuild](body) or { return error('failed to decode search results: ${err.msg()} (the answer begins: ${answer_start(body)})') } if err := data.error_text { return error('search failed: ${err}') } return data.results } // get_pkgbuild_info fetches the full record, dependencies included, of each // name, from one pkgbuild repo's site. the names are escaped likewise. no // names asks for nothing and gets it fn get_pkgbuild_info(base string, names []string) ![]PackagePkgbuild { if names.len == 0 { return []PackagePkgbuild{} } args := names.map('arg[]=${urllib.query_escape(it)}').join('&') body := pkgbuild_rpc('${base}/rpc/v5/info?${args}', 'info lookup')! data := json2.decode[PackagesPkgbuild](body) or { return error('failed to decode package info: ${err.msg()} (the answer begins: ${answer_start(body)})') } if err := data.error_text { return error('info lookup failed: ${err}') } return data.results } // pkgbuild_lookup finds name in the pkgbuild repos, tried in config order: // the first one that has the package is the one it builds from. it answers // the record, the repo's site with no trailing slash (so a clone url joins // with exactly one), and the name the config gives the repo; the site is // empty when no repo holds the name fn pkgbuild_lookup(name string) !(PackagePkgbuild, string, string) { for repo in pkgbuild_repos() { site := repo.servers[0].trim_right('/') infos := get_pkgbuild_info(site, [name])! if infos.len > 0 { return infos[0], site, repo.name } } return PackagePkgbuild{}, '', '' } // pkgbuild_has names whether any pkgbuild repo holds name fn pkgbuild_has(name string) !bool { _, site, _ := pkgbuild_lookup(name)! return site != '' }