package lib import ( "bytes" "encoding/json" "errors" "net/url" "os" "strconv" "github.com/bigkevmcd/go-configparser" "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 Deps string URL string Ver string } func LoadPackgae(repo *configparser.ConfigParser, s string) (Package, error) { var ( pkg Package err error ) pkg.Name = s pkg.Ver, err = repo.Get(s, "version") if err != nil { return pkg, err } pkg.Desc, err = repo.Get(s, "desc") if err != nil { return pkg, err } ssize, err := repo.Get(s, "size") if err != nil { return pkg, err } size, err := strconv.Atoi(ssize) if err != nil { return pkg, err } pkg.Size = SizeFromBytes(size) pkg.Deps, err = repo.Get(s, "depends") 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 } repo := configparser.New() repo.ParseReader(bytes.NewReader([]byte(list))) for _, s := range repo.Sections() { if s == "DEFAULT" { continue } pkg, err := LoadPackgae(repo, s) if err != nil { log.Errorf("Error loading %s: %s", s, 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)) }