2017-07-14 19:03:54 +02:00
|
|
|
package aur
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-07-26 14:58:51 +02:00
|
|
|
"errors"
|
2017-07-14 19:03:54 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2018-05-31 10:23:20 +02:00
|
|
|
//AURURL is the base string from which the query is built
|
|
|
|
var AURURL = "https://aur.archlinux.org/rpc.php?"
|
2017-07-14 19:03:54 +02:00
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
ResultCount int `json:"resultcount"`
|
|
|
|
Results []Pkg `json:"results"`
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:40:38 +02:00
|
|
|
//By specifies what to seach by in RPC searches
|
|
|
|
type By int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Name By = iota
|
|
|
|
NameDesc
|
|
|
|
Maintainer
|
|
|
|
Depends
|
|
|
|
MakeDepends
|
|
|
|
OptDepends
|
|
|
|
CheckDepends
|
|
|
|
)
|
|
|
|
|
|
|
|
func (by By) String() string {
|
|
|
|
switch by {
|
|
|
|
case Name:
|
|
|
|
return "name"
|
|
|
|
case NameDesc:
|
|
|
|
return "name-desc"
|
|
|
|
case Maintainer:
|
|
|
|
return "maintainer"
|
|
|
|
case Depends:
|
|
|
|
return "depends"
|
|
|
|
case MakeDepends:
|
|
|
|
return "makedepends"
|
|
|
|
case OptDepends:
|
|
|
|
return "optdepends"
|
|
|
|
case CheckDepends:
|
|
|
|
return "checkdepends"
|
|
|
|
default:
|
|
|
|
panic("invalid By")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:03:54 +02:00
|
|
|
// Pkg holds package information
|
|
|
|
type Pkg struct {
|
|
|
|
ID int `json:"ID"`
|
|
|
|
Name string `json:"Name"`
|
|
|
|
PackageBaseID int `json:"PackageBaseID"`
|
|
|
|
PackageBase string `json:"PackageBase"`
|
|
|
|
Version string `json:"Version"`
|
|
|
|
Description string `json:"Description"`
|
|
|
|
URL string `json:"URL"`
|
|
|
|
NumVotes int `json:"NumVotes"`
|
|
|
|
Popularity float64 `json:"Popularity"`
|
|
|
|
OutOfDate int `json:"OutOfDate"`
|
|
|
|
Maintainer string `json:"Maintainer"`
|
|
|
|
FirstSubmitted int `json:"FirstSubmitted"`
|
|
|
|
LastModified int `json:"LastModified"`
|
|
|
|
URLPath string `json:"URLPath"`
|
|
|
|
Depends []string `json:"Depends"`
|
|
|
|
MakeDepends []string `json:"MakeDepends"`
|
2018-02-27 02:34:24 +01:00
|
|
|
CheckDepends []string `json:"CheckDepends"`
|
2017-07-14 19:03:54 +02:00
|
|
|
Conflicts []string `json:"Conflicts"`
|
2018-03-16 01:39:49 +01:00
|
|
|
Provides []string `json:"Provides"`
|
2017-07-14 19:03:54 +02:00
|
|
|
Replaces []string `json:"Replaces"`
|
|
|
|
OptDepends []string `json:"OptDepends"`
|
2018-07-24 14:04:07 +02:00
|
|
|
Groups []string `json:"Groups"`
|
2017-07-14 19:03:54 +02:00
|
|
|
License []string `json:"License"`
|
|
|
|
Keywords []string `json:"Keywords"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func get(values url.Values) ([]Pkg, error) {
|
|
|
|
values.Set("v", "5")
|
2018-05-31 10:23:20 +02:00
|
|
|
resp, err := http.Get(AURURL + values.Encode())
|
2017-07-14 19:03:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-10 18:40:38 +02:00
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusServiceUnavailable {
|
|
|
|
return nil, errors.New("AUR is unavailable at this moment")
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:03:54 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
result := new(response)
|
|
|
|
err = dec.Decode(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Error) > 0 {
|
2018-07-26 14:58:51 +02:00
|
|
|
return nil, errors.New(result.Error)
|
2017-07-14 19:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.Results, nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 17:56:02 +01:00
|
|
|
func searchBy(query, by string) ([]Pkg, error) {
|
2017-07-14 19:03:54 +02:00
|
|
|
v := url.Values{}
|
|
|
|
v.Set("type", "search")
|
|
|
|
v.Set("arg", query)
|
|
|
|
|
2019-02-04 17:56:02 +01:00
|
|
|
if by != "" {
|
|
|
|
v.Set("by", by)
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:03:54 +02:00
|
|
|
return get(v)
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:40:38 +02:00
|
|
|
// Search searches for packages using the RPC's default search by.
|
|
|
|
// This is the same as using SearchBy With NameDesc
|
2019-02-04 17:56:02 +01:00
|
|
|
func Search(query string) ([]Pkg, error) {
|
|
|
|
return searchBy(query, "")
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:40:38 +02:00
|
|
|
// SearchBy searches for packages with a specified search by
|
|
|
|
func SearchBy(query string, by By) ([]Pkg, error) {
|
|
|
|
return searchBy(query, by.String())
|
2019-02-04 17:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Orphans() ([]Pkg, error) {
|
2019-10-10 18:40:38 +02:00
|
|
|
return SearchBy("", Maintainer)
|
2017-07-14 19:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info shows info for one or multiple packages.
|
|
|
|
func Info(pkgs []string) ([]Pkg, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("type", "info")
|
|
|
|
for _, arg := range pkgs {
|
|
|
|
v.Add("arg[]", arg)
|
|
|
|
}
|
|
|
|
return get(v)
|
|
|
|
}
|