abs.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package download
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v12/pkg/settings/exe"
  10. )
  11. const (
  12. MaxConcurrentFetch = 20
  13. absPackageURL = "https://gitlab.archlinux.org/archlinux/packaging/packages"
  14. )
  15. var (
  16. ErrInvalidRepository = errors.New(gotext.Get("invalid repository"))
  17. ErrABSPackageNotFound = errors.New(gotext.Get("package not found in repos"))
  18. )
  19. // Return format for pkgbuild
  20. // https://gitlab.archlinux.org/archlinux/packaging/packages/0ad/-/raw/main/PKGBUILD
  21. func getPackagePKGBUILDURL(pkgName string) string {
  22. return fmt.Sprintf("%s/%s/-/raw/main/PKGBUILD", absPackageURL, pkgName)
  23. }
  24. // Return format for pkgbuild repo
  25. // https://gitlab.archlinux.org/archlinux/packaging/packages/0ad.git
  26. func getPackageRepoURL(pkgName string) string {
  27. return fmt.Sprintf("%s/%s.git", absPackageURL, pkgName)
  28. }
  29. // ABSPKGBUILD retrieves the PKGBUILD file to a dest directory.
  30. func ABSPKGBUILD(httpClient httpRequestDoer, dbName, pkgName string) ([]byte, error) {
  31. packageURL := getPackagePKGBUILDURL(pkgName)
  32. resp, err := httpClient.Get(packageURL)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if resp.StatusCode != http.StatusOK {
  37. return nil, ErrABSPackageNotFound
  38. }
  39. defer resp.Body.Close()
  40. pkgBuild, err := io.ReadAll(resp.Body)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return pkgBuild, nil
  45. }
  46. // ABSPKGBUILDRepo retrieves the PKGBUILD repository to a dest directory.
  47. func ABSPKGBUILDRepo(ctx context.Context, cmdBuilder exe.GitCmdBuilder,
  48. dbName, pkgName, dest string, force bool,
  49. ) (bool, error) {
  50. pkgURL := getPackageRepoURL(pkgName)
  51. return downloadGitRepo(ctx, cmdBuilder, pkgURL,
  52. pkgName, dest, force, "--single-branch")
  53. }