brunel/config/config.go
2024-08-02 21:35:32 +01:00

175 lines
3.8 KiB
Go

package config
import (
"brunel/domain"
"fmt"
"io"
"net/http"
"os"
"github.com/goccy/go-json"
)
var Users map[string]domain.User
var Configs Config
// Struct representing an individual package file entries
type PackageFile struct {
Name string `json:"name"`
Url string `json:"url"`
Subrepos []string `json:"subrepos"`
Priority int `json:"priority"`
UseWhitelist bool `json:"usewhitelist"`
Whitelist []string `json:"whitelist"`
Blacklist []string `json:"blacklist"`
Packagepath string `json:"packagepath"`
Compression string `json:"compression"`
}
// Struct for the overall configuration
type Config struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
UpstreamFallback bool `json:"upstreamFallback"`
LocalPackageFiles []PackageFile `json:"localPackageFiles"`
ExternalPackageFiles []PackageFile `json:"externalPackageFiles"`
LTOBlocklist []string `json:"ltoBlocklist"`
I386List []string `json:"i386List"`
DeboutputDir string `json:"deboutputDir"`
Salt string `json:"salt"`
Buildrepo string `json:"buildRepo"`
Buildbranch string `json:"buildBranch"`
GitCache string `json:"gitCache"`
GitToken string `json:"gitToken"`
GitUser string `json:"gitUser"`
ActionsUrl string `json:"actionsUrl"`
}
type expPkgNames struct {
PackageNames []string `json:"package_names"`
}
type i386List struct {
PackageNames []string `json:"i386_whitelist"`
}
func Init() error {
err := loadUsers()
if err != nil {
return err
}
err = loadConfig()
if err != nil {
return err
}
return nil
}
func loadUsers() error {
jsonFile, err := os.Open("users.json")
if err != nil {
fmt.Println(err)
return err
}
defer jsonFile.Close()
byteValue, _ := io.ReadAll(jsonFile)
var users []domain.User
err = json.Unmarshal(byteValue, &users)
if err != nil {
fmt.Println(err)
return err
}
var usersMap = make(map[string]domain.User, len(users))
for _, user := range users {
usersMap[user.Username] = user
}
Users = usersMap
return nil
}
func loadConfig() error {
jsonFile, err := os.Open("config.json")
if err != nil {
fmt.Println(err)
return err
}
defer jsonFile.Close()
byteValue, _ := io.ReadAll(jsonFile)
var config Config
err = json.Unmarshal(byteValue, &config)
if err != nil {
fmt.Println(err)
return err
}
config.I386List, err = loadI386List()
if err != nil {
return err
}
Configs = config
xpPkgNames, err := loadExperimentalPackageNames()
if err != nil {
return err
}
for i, pkg := range Configs.ExternalPackageFiles {
if pkg.Name == "Debian experimental" {
pkg.Whitelist = xpPkgNames
pkg.UseWhitelist = true
Configs.ExternalPackageFiles[i] = pkg
break
}
}
return nil
}
func loadI386List() ([]string, error) {
var i386List i386List
response, err := http.Get("https://raw.githubusercontent.com/PikaOS-Linux/pika-base-debian-container/main/i386_src_whitelist.json")
if err != nil {
return nil, err
}
defer response.Body.Close()
byteValue, _ := io.ReadAll(response.Body)
err = json.Unmarshal(byteValue, &i386List)
if err != nil {
fmt.Println(err)
return nil, err
}
return i386List.PackageNames, nil
}
func loadExperimentalPackageNames() ([]string, error) {
var epPkgNames expPkgNames
response, err := http.Get("https://raw.githubusercontent.com/PikaOS-Linux/pika-base-debian-container/main/exp_pkg_names.json")
if err != nil {
return nil, err
}
defer response.Body.Close()
byteValue, _ := io.ReadAll(response.Body)
err = json.Unmarshal(byteValue, &epPkgNames)
if err != nil {
fmt.Println(err)
return nil, err
}
return epPkgNames.PackageNames, nil
}