aur.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package aur
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. )
  8. const aurURL = "https://aur.archlinux.org/rpc.php?"
  9. type response struct {
  10. Error string `json:"error"`
  11. Version int `json:"version"`
  12. Type string `json:"type"`
  13. ResultCount int `json:"resultcount"`
  14. Results []Pkg `json:"results"`
  15. }
  16. // Pkg holds package information
  17. type Pkg struct {
  18. ID int `json:"ID"`
  19. Name string `json:"Name"`
  20. PackageBaseID int `json:"PackageBaseID"`
  21. PackageBase string `json:"PackageBase"`
  22. Version string `json:"Version"`
  23. Description string `json:"Description"`
  24. URL string `json:"URL"`
  25. NumVotes int `json:"NumVotes"`
  26. Popularity float64 `json:"Popularity"`
  27. OutOfDate int `json:"OutOfDate"`
  28. Maintainer string `json:"Maintainer"`
  29. FirstSubmitted int `json:"FirstSubmitted"`
  30. LastModified int `json:"LastModified"`
  31. URLPath string `json:"URLPath"`
  32. Depends []string `json:"Depends"`
  33. MakeDepends []string `json:"MakeDepends"`
  34. CheckDepends []string `json:"CheckDepends"`
  35. Conflicts []string `json:"Conflicts"`
  36. Provides []string `json:"Provides"`
  37. Replaces []string `json:"Replaces"`
  38. OptDepends []string `json:"OptDepends"`
  39. License []string `json:"License"`
  40. Keywords []string `json:"Keywords"`
  41. }
  42. func get(values url.Values) ([]Pkg, error) {
  43. values.Set("v", "5")
  44. resp, err := http.Get(aurURL + values.Encode())
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer resp.Body.Close()
  49. dec := json.NewDecoder(resp.Body)
  50. result := new(response)
  51. err = dec.Decode(result)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if len(result.Error) > 0 {
  56. return nil, fmt.Errorf(result.Error)
  57. }
  58. return result.Results, nil
  59. }
  60. // Search searches for packages by package name.
  61. func Search(query string) ([]Pkg, error) {
  62. v := url.Values{}
  63. v.Set("type", "search")
  64. v.Set("arg", query)
  65. return get(v)
  66. }
  67. // SearchByNameDesc searches for package by package name and description.
  68. func SearchByNameDesc(query string) ([]Pkg, error) {
  69. v := url.Values{}
  70. v.Set("type", "search")
  71. v.Set("by", "name-desc")
  72. v.Set("arg", query)
  73. return get(v)
  74. }
  75. // SearchByMaintainer searches for package by maintainer.
  76. func SearchByMaintainer(query string) ([]Pkg, error) {
  77. v := url.Values{}
  78. v.Set("type", "search")
  79. v.Set("by", "maintainer")
  80. v.Set("arg", query)
  81. return get(v)
  82. }
  83. // Info shows info for one or multiple packages.
  84. func Info(pkgs []string) ([]Pkg, error) {
  85. v := url.Values{}
  86. v.Set("type", "info")
  87. for _, arg := range pkgs {
  88. v.Add("arg[]", arg)
  89. }
  90. return get(v)
  91. }