2020-12-15 01:29:05 +01:00
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2020-12-15 01:29:05 +01:00
|
|
|
"errors"
|
2021-07-25 12:10:51 +02:00
|
|
|
"fmt"
|
2021-08-26 09:31:14 +02:00
|
|
|
"io"
|
2020-12-15 01:29:05 +01:00
|
|
|
"net/http"
|
2023-05-25 11:00:33 +02:00
|
|
|
"regexp"
|
2021-01-30 12:52:50 +01:00
|
|
|
|
|
|
|
"github.com/leonelquinteros/gotext"
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/exe"
|
2020-12-15 01:29:05 +01:00
|
|
|
)
|
|
|
|
|
2021-07-25 12:10:51 +02:00
|
|
|
const (
|
|
|
|
MaxConcurrentFetch = 20
|
2023-05-22 22:22:49 +02:00
|
|
|
absPackageURL = "https://gitlab.archlinux.org/archlinux/packaging/packages"
|
2021-07-25 12:10:51 +02:00
|
|
|
)
|
2021-01-30 12:52:50 +01:00
|
|
|
|
2021-07-25 12:10:51 +02:00
|
|
|
var (
|
|
|
|
ErrInvalidRepository = errors.New(gotext.Get("invalid repository"))
|
|
|
|
ErrABSPackageNotFound = errors.New(gotext.Get("package not found in repos"))
|
|
|
|
)
|
2020-12-15 01:29:05 +01:00
|
|
|
|
2023-05-25 11:00:33 +02:00
|
|
|
type regexReplace struct {
|
|
|
|
repl string
|
|
|
|
match *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
// regex replacements for Gitlab URLs
|
|
|
|
// info: https://gitlab.archlinux.org/archlinux/devtools/-/blob/6ce666a1669235749c17d5c44d8a24dea4a135da/src/lib/api/gitlab.sh#L84
|
|
|
|
var gitlabRepl = []regexReplace{
|
|
|
|
{repl: `$1-$2`, match: regexp.MustCompile(`([a-zA-Z0-9]+)\+([a-zA-Z]+)`)},
|
|
|
|
{repl: `plus`, match: regexp.MustCompile(`\+`)},
|
|
|
|
{repl: `-`, match: regexp.MustCompile(`[^a-zA-Z0-9_\-.]`)},
|
|
|
|
{repl: `-`, match: regexp.MustCompile(`[_\-]{2,}`)},
|
|
|
|
{repl: `unix-tree`, match: regexp.MustCompile(`^tree$`)},
|
|
|
|
}
|
|
|
|
|
2021-08-02 18:00:50 +02:00
|
|
|
// Return format for pkgbuild
|
2023-05-22 22:22:49 +02:00
|
|
|
// https://gitlab.archlinux.org/archlinux/packaging/packages/0ad/-/raw/main/PKGBUILD
|
|
|
|
func getPackagePKGBUILDURL(pkgName string) string {
|
2023-05-25 11:00:33 +02:00
|
|
|
return fmt.Sprintf("%s/%s/-/raw/main/PKGBUILD", absPackageURL, convertPkgNameForURL(pkgName))
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return format for pkgbuild repo
|
2023-05-22 22:22:49 +02:00
|
|
|
// https://gitlab.archlinux.org/archlinux/packaging/packages/0ad.git
|
|
|
|
func getPackageRepoURL(pkgName string) string {
|
2023-05-25 11:00:33 +02:00
|
|
|
return fmt.Sprintf("%s/%s.git", absPackageURL, convertPkgNameForURL(pkgName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert pkgName for Gitlab URL path (repo name)
|
|
|
|
func convertPkgNameForURL(pkgName string) string {
|
|
|
|
for _, regex := range gitlabRepl {
|
|
|
|
pkgName = regex.match.ReplaceAllString(pkgName, regex.repl)
|
|
|
|
}
|
|
|
|
return pkgName
|
2020-12-15 01:29:05 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 18:05:36 +02:00
|
|
|
// ABSPKGBUILD retrieves the PKGBUILD file to a dest directory.
|
2021-08-11 22:05:47 +02:00
|
|
|
func ABSPKGBUILD(httpClient httpRequestDoer, dbName, pkgName string) ([]byte, error) {
|
2023-05-22 22:22:49 +02:00
|
|
|
packageURL := getPackagePKGBUILDURL(pkgName)
|
2020-12-15 01:29:05 +01:00
|
|
|
|
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-08-02 18:00:50 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-01-30 12:52:50 +01:00
|
|
|
return nil, ErrABSPackageNotFound
|
|
|
|
}
|
|
|
|
|
2020-12-15 01:29:05 +01:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-08-26 09:31:14 +02:00
|
|
|
pkgBuild, err := io.ReadAll(resp.Body)
|
2020-12-15 01:29:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkgBuild, nil
|
|
|
|
}
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2021-08-02 21:56:19 +02:00
|
|
|
// ABSPKGBUILDRepo retrieves the PKGBUILD repository to a dest directory.
|
2021-08-12 18:56:23 +02:00
|
|
|
func ABSPKGBUILDRepo(ctx context.Context, cmdBuilder exe.GitCmdBuilder,
|
2023-01-03 22:43:56 +01:00
|
|
|
dbName, pkgName, dest string, force bool,
|
|
|
|
) (bool, error) {
|
2023-05-22 22:22:49 +02:00
|
|
|
pkgURL := getPackageRepoURL(pkgName)
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
return downloadGitRepo(ctx, cmdBuilder, pkgURL,
|
2023-05-22 22:22:49 +02:00
|
|
|
pkgName, dest, force, "--single-branch")
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|