yay/pkg/download/aur.go

96 lines
1.9 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"
"fmt"
"io"
2020-12-15 01:29:05 +01:00
"net/http"
"net/url"
"sync"
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/multierror"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/text"
2020-12-15 01:29:05 +01:00
)
2021-08-11 22:05:47 +02:00
func AURPKGBUILD(httpClient httpRequestDoer, pkgName, aurURL string) ([]byte, error) {
2020-12-15 01:29:05 +01:00
values := url.Values{}
values.Set("h", pkgName)
pkgURL := aurURL + "/cgit/aur.git/plain/PKGBUILD?" + values.Encode()
2020-12-15 01:29:05 +01:00
2021-01-30 12:52:50 +01:00
resp, err := httpClient.Get(pkgURL)
2020-12-15 01:29:05 +01:00
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, ErrAURPackageNotFound{pkgName: pkgName}
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
}
// AURPkgbuildRepo retrieves the PKGBUILD repository to a dest directory.
2021-08-12 18:56:23 +02:00
func AURPKGBUILDRepo(ctx context.Context, cmdBuilder exe.GitCmdBuilder, aurURL, pkgName, dest string, force bool) (bool, error) {
pkgURL := fmt.Sprintf("%s/%s.git", aurURL, pkgName)
2021-08-12 18:56:23 +02:00
return downloadGitRepo(ctx, cmdBuilder, pkgURL, pkgName, dest, force)
}
func AURPKGBUILDRepos(
2021-08-12 18:56:23 +02:00
ctx context.Context,
cmdBuilder exe.GitCmdBuilder,
targets []string, aurURL, dest string, force bool) (map[string]bool, error) {
cloned := make(map[string]bool, len(targets))
var (
mux sync.Mutex
errs multierror.MultiError
wg sync.WaitGroup
)
sem := make(chan uint8, MaxConcurrentFetch)
for _, target := range targets {
sem <- 1
wg.Add(1)
go func(target string) {
2021-08-12 18:56:23 +02:00
newClone, err := AURPKGBUILDRepo(ctx, cmdBuilder, aurURL, target, dest, force)
progress := 0
2021-08-11 20:13:28 +02:00
if err != nil {
errs.Add(err)
} else {
mux.Lock()
cloned[target] = newClone
progress = len(cloned)
mux.Unlock()
}
text.OperationInfoln(
gotext.Get("(%d/%d) Downloaded PKGBUILD: %s",
progress, len(targets), text.Cyan(target)))
<-sem
wg.Done()
}(target)
}
wg.Wait()
return cloned, errs.Return()
}