Files
pkgstash/config.go
T
2026-04-21 08:18:27 -06:00

62 lines
1.3 KiB
Go

package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type Config struct {
CacheRoot string `toml:"cache_root"`
MirrorURLs []string `toml:"mirror_urls"`
MirroredRepos []string `toml:"mirrored_repos"`
Port string `toml:"port"`
Auth AuthConfig `toml:"auth"`
}
type AuthConfig struct {
Token string `toml:"token"`
}
/* Function kept for reference for future logic
func NewConfig() *Config {
return &Config{
CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURLs: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
Auth: AuthConfig{Token: "FakeToken"},
}
}
*/
func ReadConfig(path string) (*Config, error) {
var cfg Config
_, err := toml.DecodeFile(path, &cfg)
if err != nil {
return nil, fmt.Errorf("error loading config from %s: %w", path, err)
}
if err = cfg.validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return &cfg, nil
}
func (c *Config) validate() error {
if c.CacheRoot == "" {
return fmt.Errorf("cache root is required")
}
if len(c.MirrorURLs) == 0 {
return fmt.Errorf("at least one mirror is required")
}
if c.Port == "" {
c.Port = "8090"
}
if c.Auth.Token == "" {
return fmt.Errorf("auth token is required")
}
return nil
}