abs.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package download
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/leonelquinteros/gotext"
  8. "github.com/Jguer/yay/v10/pkg/settings/exe"
  9. )
  10. const (
  11. MaxConcurrentFetch = 20
  12. _urlPackagePath = "%s/raw/packages/%s/trunk/PKGBUILD"
  13. )
  14. var (
  15. ErrInvalidRepository = errors.New(gotext.Get("invalid repository"))
  16. ErrABSPackageNotFound = errors.New(gotext.Get("package not found in repos"))
  17. ABSPackageURL = "https://github.com/archlinux/svntogit-packages"
  18. ABSCommunityURL = "https://github.com/archlinux/svntogit-community"
  19. )
  20. func getRepoURL(db string) (string, error) {
  21. switch db {
  22. case "core", "extra", "testing":
  23. return ABSPackageURL, nil
  24. case "community", "multilib", "community-testing", "multilib-testing":
  25. return ABSCommunityURL, nil
  26. }
  27. return "", ErrInvalidRepository
  28. }
  29. // Return format for pkgbuild
  30. // https://github.com/archlinux/svntogit-community/raw/packages/neovim/trunk/PKGBUILD
  31. func getPackageURL(db, pkgName string) (string, error) {
  32. repoURL, err := getRepoURL(db)
  33. if err != nil {
  34. return "", err
  35. }
  36. return fmt.Sprintf(_urlPackagePath, repoURL, pkgName), err
  37. }
  38. // Return format for pkgbuild repo
  39. // https://github.com/archlinux/svntogit-community.git
  40. func getPackageRepoURL(db string) (string, error) {
  41. repoURL, err := getRepoURL(db)
  42. if err != nil {
  43. return "", err
  44. }
  45. return repoURL + ".git", err
  46. }
  47. // ABSPKGBUILD retrieves the PKGBUILD file to a dest directory.
  48. func ABSPKGBUILD(httpClient *http.Client, dbName, pkgName string) ([]byte, error) {
  49. packageURL, err := getPackageURL(dbName, pkgName)
  50. if err != nil {
  51. return nil, err
  52. }
  53. resp, err := httpClient.Get(packageURL)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if resp.StatusCode != http.StatusOK {
  58. return nil, ErrABSPackageNotFound
  59. }
  60. defer resp.Body.Close()
  61. pkgBuild, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return pkgBuild, nil
  66. }
  67. // ABSPKGBUILDRepo retrieves the PKGBUILD repository to a dest directory.
  68. func ABSPKGBUILDRepo(cmdBuilder exe.GitCmdBuilder, dbName, pkgName, dest string, force bool) (bool, error) {
  69. pkgURL, err := getPackageRepoURL(dbName)
  70. if err != nil {
  71. return false, err
  72. }
  73. return downloadGitRepo(cmdBuilder, pkgURL,
  74. pkgName, dest, force, "--single-branch", "-b", "packages/"+pkgName)
  75. }