aur.go 2.6 KB

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