yay/pkg/download/unified.go

75 lines
1.5 KiB
Go
Raw Normal View History

2020-12-15 01:29:05 +01:00
package download
import (
2021-01-30 12:52:50 +01:00
"net/http"
2020-12-15 01:29:05 +01:00
"sync"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/multierror"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/text"
)
2021-02-17 10:33:56 +01:00
func getURLName(pkg db.IPackage) string {
2020-12-15 01:29:05 +01:00
name := pkg.Base()
if name == "" {
name = pkg.Name()
}
2020-12-15 01:29:05 +01:00
return name
}
2021-01-30 12:52:50 +01:00
func GetPkgbuilds(dbExecutor db.Executor, httpClient *http.Client, targets []string, mode settings.TargetMode) (map[string][]byte, error) {
2020-12-15 01:29:05 +01:00
pkgbuilds := make(map[string][]byte, len(targets))
var mux sync.Mutex
var errs multierror.MultiError
var wg sync.WaitGroup
sem := make(chan uint8, MaxConcurrentFetch)
for _, target := range targets {
aur := true
dbName, name := text.SplitDBFromName(target)
if dbName != "aur" && (mode == settings.ModeAny || mode == settings.ModeRepo) {
if pkg := dbExecutor.SyncPackage(name); pkg != nil {
2020-12-15 01:29:05 +01:00
aur = false
name = getURLName(pkg)
dbName = pkg.DB().Name()
}
}
if aur && mode == settings.ModeRepo {
// Mode does not allow AUR packages
continue
}
sem <- 1
wg.Add(1)
go func(target, dbName, pkgName string, aur bool) {
var err error
var pkgbuild []byte
if aur {
2021-01-30 12:52:50 +01:00
pkgbuild, err = GetAURPkgbuild(httpClient, pkgName)
2020-12-15 01:29:05 +01:00
} else {
2021-01-30 12:52:50 +01:00
pkgbuild, err = GetABSPkgbuild(httpClient, dbName, pkgName)
2020-12-15 01:29:05 +01:00
}
if err == nil {
mux.Lock()
pkgbuilds[target] = pkgbuild
mux.Unlock()
} else {
errs.Add(err)
}
<-sem
wg.Done()
}(target, dbName, name, aur)
}
wg.Wait()
2020-12-15 01:29:05 +01:00
return pkgbuilds, errs.Return()
}