brunel/config/config.go
2024-07-29 22:40:26 +01:00

108 lines
2.3 KiB
Go

package config
import (
"brunel/domain"
"fmt"
"io"
"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"`
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"`
}
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
}
Configs = config
return nil
}