2024-01-17 17:06:26 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
2024-02-23 18:31:38 +00:00
|
|
|
"bytes"
|
2024-01-17 17:06:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2024-02-23 18:31:38 +00:00
|
|
|
"strconv"
|
2024-01-17 17:06:26 +00:00
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
"github.com/bigkevmcd/go-configparser"
|
2024-01-17 17:06:26 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-01-17 17:54:37 +00:00
|
|
|
Deps string
|
2024-01-17 17:06:26 +00:00
|
|
|
URL string
|
|
|
|
Ver string
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:31:38 +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
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
pkg.Ver, err = repo.Get(s, "version")
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return pkg, err
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
pkg.Desc, err = repo.Get(s, "desc")
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return pkg, err
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
ssize, err := repo.Get(s, "size")
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return pkg, err
|
|
|
|
}
|
2024-02-23 18:31:38 +00:00
|
|
|
|
|
|
|
size, err := strconv.Atoi(ssize)
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return pkg, err
|
|
|
|
}
|
|
|
|
pkg.Size = SizeFromBytes(size)
|
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
pkg.Deps, err = repo.Get(s, "depends")
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return pkg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
repo := configparser.New()
|
|
|
|
repo.ParseReader(bytes.NewReader([]byte(list)))
|
|
|
|
|
2024-01-17 17:06:26 +00:00
|
|
|
for _, s := range repo.Sections() {
|
2024-02-23 18:31:38 +00:00
|
|
|
if s == "DEFAULT" {
|
2024-01-17 17:06:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:31:38 +00:00
|
|
|
pkg, err := LoadPackgae(repo, s)
|
2024-01-17 17:06:26 +00:00
|
|
|
if err != nil {
|
2024-02-23 18:31:38 +00:00
|
|
|
log.Errorf("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 {
|
|
|
|
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))
|
|
|
|
}
|