tracker/lib/repo.go

164 lines
2.9 KiB
Go
Raw Normal View History

2024-01-17 17:06:26 +00:00
package lib
import (
"bytes"
2024-01-17 17:06:26 +00:00
"encoding/json"
"errors"
2024-03-19 20:11:43 +00:00
"log"
2024-01-17 17:06:26 +00:00
"net/url"
"os"
"strconv"
2024-03-21 18:52:14 +00:00
"strings"
2024-01-17 17:06:26 +00:00
"github.com/bigkevmcd/go-configparser"
2024-01-17 17:06:26 +00:00
"github.com/gofiber/fiber/v2"
)
type Config struct {
Repos []Repo `json:"repos"`
}
var Repos []Repo
type Repo struct {
Source string `json:"source"`
Branch string `json:"branch"`
Name string `json:"name"`
URL string `json:"url"`
}
var Packages []Package
type Package struct {
2024-03-21 18:52:14 +00:00
Name string `json:"name"`
Repo string `json:"repo"`
Desc string `json:"desc"`
Size string `json:"size"`
Deps []string `json:"depends"`
URL string `json:"url"`
Ver string `json:"version"`
}
func (p Package) StrDeps() string {
return ListToStr(p.Deps)
2024-01-17 17:06:26 +00:00
}
func LoadPackgae(repo *configparser.ConfigParser, s string) (Package, error) {
var (
pkg Package
err error
)
pkg.Name = s
2024-01-17 17:06:26 +00:00
pkg.Ver, err = repo.Get(s, "version")
2024-01-17 17:06:26 +00:00
if err != nil {
return pkg, err
}
pkg.Desc, err = repo.Get(s, "desc")
2024-01-17 17:06:26 +00:00
if err != nil {
return pkg, err
}
ssize, err := repo.Get(s, "size")
2024-01-17 17:06:26 +00:00
if err != nil {
return pkg, err
}
size, err := strconv.Atoi(ssize)
2024-01-17 17:06:26 +00:00
if err != nil {
return pkg, err
}
pkg.Size = SizeFromBytes(size)
2024-03-21 18:52:14 +00:00
deps, err := repo.Get(s, "depends")
2024-01-17 17:06:26 +00:00
if err != nil {
return pkg, err
}
2024-03-21 18:52:14 +00:00
if deps == "" {
pkg.Deps = []string{}
}else {
pkg.Deps = strings.Split(deps, "\n")
}
2024-01-17 17:06:26 +00:00
return pkg, nil
}
func GetRepo(r Repo) ([]Package, error){
var pkgs []Package
furl, err := url.JoinPath(r.URL, r.Name+".tar.gz")
if err != nil {
return pkgs, err
}
agent := fiber.Get(furl)
code, body, errs := agent.Bytes()
if len(errs) > 0 {
return pkgs, errors.New("Request failed")
}
if code != 200 {
return pkgs, errors.New("Bad response")
}
list, err := ExtractFile(body, "pkgs")
if err != nil {
return pkgs, err
}
repo := configparser.New()
repo.ParseReader(bytes.NewReader([]byte(list)))
2024-01-17 17:06:26 +00:00
for _, s := range repo.Sections() {
if s == "DEFAULT" {
2024-01-17 17:06:26 +00:00
continue
}
pkg, err := LoadPackgae(repo, s)
2024-01-17 17:06:26 +00:00
if err != nil {
2024-03-19 20:11:43 +00:00
log.Printf("Error loading %s: %s", s, err)
2024-01-17 17:06:26 +00:00
continue
}
pkg.Repo = r.Name
pkg.URL, err = url.JoinPath(
r.Source, "src/branch/"+r.Branch+"/src", pkg.Name, "pkg.sh")
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}
func LoadRepos() {
Repos = []Repo{}
data, err := os.ReadFile("cfg.json")
if err != nil {
2024-03-19 20:11:43 +00:00
log.Printf("Failed to read the configuration")
2024-01-17 17:06:26 +00:00
}
var cfg Config
err = json.Unmarshal(data, &cfg)
if err != nil {
2024-03-19 20:11:43 +00:00
log.Printf("Failed to parse the configuration: %s", err)
2024-01-17 17:06:26 +00:00
}
Repos = cfg.Repos
}
func LoadAllPkgs() {
LoadRepos()
Packages = []Package{}
for _, r := range Repos {
pkgs, err := GetRepo(r)
if err != nil {
2024-03-19 20:11:43 +00:00
log.Printf("Error loading %s: %s", r.Name, err.Error())
2024-01-17 17:06:26 +00:00
continue
}
Packages = append(Packages, pkgs...)
}
2024-03-19 20:11:43 +00:00
log.Printf("Loaded total %d packages", len(Packages))
2024-01-17 17:06:26 +00:00
}