yay/pkg/download/abs.go

97 lines
2.3 KiB
Go
Raw Normal View History

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"
"fmt"
"io"
2020-12-15 01:29:05 +01:00
"net/http"
2021-01-30 12:52:50 +01:00
"github.com/leonelquinteros/gotext"
2021-09-08 22:28:08 +02:00
"github.com/Jguer/yay/v11/pkg/settings/exe"
2020-12-15 01:29:05 +01:00
)
const (
MaxConcurrentFetch = 20
_urlPackagePath = "%s/raw/packages/%s/trunk/PKGBUILD"
)
2021-01-30 12:52:50 +01:00
var (
ErrInvalidRepository = errors.New(gotext.Get("invalid repository"))
ErrABSPackageNotFound = errors.New(gotext.Get("package not found in repos"))
ABSPackageURL = "https://github.com/archlinux/svntogit-packages"
ABSCommunityURL = "https://github.com/archlinux/svntogit-community"
)
2020-12-15 01:29:05 +01:00
func getRepoURL(db string) (string, error) {
2020-12-15 01:29:05 +01:00
switch db {
case "core", "extra", "testing":
return ABSPackageURL, nil
2020-12-15 01:29:05 +01:00
case "community", "multilib", "community-testing", "multilib-testing":
return ABSCommunityURL, nil
}
return "", ErrInvalidRepository
}
// Return format for pkgbuild
// https://github.com/archlinux/svntogit-community/raw/packages/neovim/trunk/PKGBUILD
func getPackageURL(db, pkgName string) (string, error) {
repoURL, err := getRepoURL(db)
if err != nil {
return "", err
2020-12-15 01:29:05 +01:00
}
return fmt.Sprintf(_urlPackagePath, repoURL, pkgName), err
}
// Return format for pkgbuild repo
// https://github.com/archlinux/svntogit-community.git
func getPackageRepoURL(db string) (string, error) {
repoURL, err := getRepoURL(db)
if err != nil {
return "", err
}
return repoURL + ".git", err
2020-12-15 01:29:05 +01: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) {
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
}
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()
pkgBuild, err := io.ReadAll(resp.Body)
2020-12-15 01:29:05 +01:00
if err != nil {
return nil, err
}
return pkgBuild, nil
}
// 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,
dbName, pkgName, dest string, force bool) (bool, error) {
pkgURL, err := getPackageRepoURL(dbName)
if err != nil {
return false, err
}
2021-08-12 18:56:23 +02:00
return downloadGitRepo(ctx, cmdBuilder, pkgURL,
pkgName, dest, force, "--single-branch", "-b", "packages/"+pkgName)
}