yay/pkg/download/abs.go

58 lines
1.3 KiB
Go
Raw Normal View History

2020-12-15 01:29:05 +01:00
package download
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
2021-01-30 12:52:50 +01:00
"github.com/leonelquinteros/gotext"
2020-12-15 01:29:05 +01:00
)
2021-01-30 12:52:50 +01:00
var ErrInvalidRepository = errors.New(gotext.Get("invalid repository"))
var ErrABSPackageNotFound = errors.New(gotext.Get("package not found in repos"))
2020-12-15 01:29:05 +01:00
const MaxConcurrentFetch = 20
2021-01-30 12:52:50 +01:00
const urlPackagePath = "/plain/trunk/PKGBUILD?"
var ABSPackageURL = "https://git.archlinux.org/svntogit/packages.git"
var ABSCommunityURL = "https://git.archlinux.org/svntogit/community.git"
2020-12-15 01:29:05 +01:00
func getPackageURL(db, pkgName string) (string, error) {
values := url.Values{}
values.Set("h", "packages/"+pkgName)
nameEncoded := values.Encode()
switch db {
case "core", "extra", "testing":
2021-01-30 12:52:50 +01:00
return ABSPackageURL + urlPackagePath + nameEncoded, nil
2020-12-15 01:29:05 +01:00
case "community", "multilib", "community-testing", "multilib-testing":
2021-01-30 12:52:50 +01:00
return ABSCommunityURL + urlPackagePath + nameEncoded, nil
2020-12-15 01:29:05 +01:00
}
return "", ErrInvalidRepository
}
2021-01-30 12:52:50 +01:00
func GetABSPkgbuild(httpClient *http.Client, dbName, pkgName string) ([]byte, error) {
2020-12-15 01:29:05 +01:00
packageURL, err := getPackageURL(dbName, pkgName)
if err != nil {
return nil, err
}
2021-01-30 12:52:50 +01:00
resp, err := httpClient.Get(packageURL)
2020-12-15 01:29:05 +01:00
if err != nil {
return nil, err
}
2021-01-30 12:52:50 +01:00
if resp.StatusCode != 200 {
return nil, ErrABSPackageNotFound
}
2020-12-15 01:29:05 +01:00
defer resp.Body.Close()
pkgBuild, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return pkgBuild, nil
}