1
0
Fork 0
squid-rewriter/distro/fedora.go

66 lines
1.2 KiB
Go

package distro
import (
"bufio"
"fmt"
"net/http"
"regexp"
"strings"
"sync"
)
var repoPathByName map[string]string = map[string]string{
"main": "fedora/",
"epel": "epel/",
"centos": "centos/",
}
type Fedora struct{}
func (d Fedora) GetName() string {
return "fedora"
}
func (d Fedora) GetRepos() []string {
r := []string{}
for k := range repoPathByName {
r = append(r, k)
}
return r
}
func (d Fedora) FetchMirrors(repos []string, repoMirrors chan<- RepoMirror, done *sync.WaitGroup) {
defer done.Done()
re := regexp.MustCompile(`^\S*((https?|ftp)://.*)\S*$`)
for _, repoName := range repos {
repoPath, ok := repoPathByName[repoName]
if !ok {
continue
}
url := fmt.Sprintf("http://mirrors.fedoraproject.org/mirrorlist?path=%s", repoPath)
done.Add(1)
go func(u string, rn string) {
defer done.Done()
resp, err := http.Get(u)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
m := re.FindSubmatch([]byte(line))
if len(m) > 1 {
repoMirrors <- RepoMirror{d.GetName(), rn, string(m[1])}
}
}
}
}(url, repoName)
}
}