154 lines
2.6 KiB
Go
154 lines
2.6 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
"os"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/log"
|
|
"gopkg.in/ini.v1"
|
|
)
|
|
|
|
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 {
|
|
Name string
|
|
Repo string
|
|
Desc string
|
|
Size string
|
|
Sum string
|
|
URL string
|
|
Ver string
|
|
}
|
|
|
|
func LoadPackgae(sec *ini.Section) (Package, error) {
|
|
var pkg Package
|
|
pkg.Name = sec.Name()
|
|
|
|
k, err := sec.GetKey("version")
|
|
if err != nil {
|
|
return pkg, err
|
|
}
|
|
pkg.Ver = k.String()
|
|
|
|
k, err = sec.GetKey("desc")
|
|
if err != nil {
|
|
return pkg, err
|
|
}
|
|
pkg.Desc = k.String()
|
|
|
|
k, err = sec.GetKey("size")
|
|
if err != nil {
|
|
return pkg, err
|
|
}
|
|
|
|
size, err := k.Int()
|
|
if err != nil {
|
|
return pkg, err
|
|
}
|
|
pkg.Size = SizeFromBytes(size)
|
|
|
|
k, err = sec.GetKey("sum")
|
|
if err != nil {
|
|
return pkg, err
|
|
}
|
|
pkg.Sum = k.String()
|
|
|
|
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 := ini.Empty()
|
|
err = repo.Append([]byte(list))
|
|
if err != nil {
|
|
return pkgs, err
|
|
}
|
|
|
|
for _, s := range repo.Sections() {
|
|
if s.Name() == "DEFAULT" {
|
|
continue
|
|
}
|
|
|
|
pkg, err := LoadPackgae(s)
|
|
if err != nil {
|
|
log.Errorf("Error loading %s: %s", s.Name(), err)
|
|
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 {
|
|
log.Error("Failed to read the configuration")
|
|
}
|
|
|
|
var cfg Config
|
|
err = json.Unmarshal(data, &cfg)
|
|
if err != nil {
|
|
log.Errorf("Failed to parse the configuration: %s", err)
|
|
}
|
|
|
|
Repos = cfg.Repos
|
|
}
|
|
|
|
func LoadAllPkgs() {
|
|
LoadRepos()
|
|
Packages = []Package{}
|
|
|
|
for _, r := range Repos {
|
|
pkgs, err := GetRepo(r)
|
|
if err != nil {
|
|
log.Errorf("Error loading %s: %s", r.Name, err.Error())
|
|
continue
|
|
}
|
|
Packages = append(Packages, pkgs...)
|
|
}
|
|
|
|
log.Infof("Loaded total %d packages", len(Packages))
|
|
}
|