read config from file

This commit is contained in:
2026-04-15 14:22:05 -06:00
parent 58f9093406
commit 87d52aae58
3 changed files with 17 additions and 14 deletions
+14 -11
View File
@@ -7,22 +7,22 @@ import (
)
type Config struct {
MirrorRoot string
MirrorURL string
Port string
Auth AuthConfig
CacheRoot string `toml:"cache_root"`
MirrorURL string `toml:"mirror_url"`
Port string `toml:"port"`
Auth AuthConfig `toml:"auth"`
}
type AuthConfig struct {
Token string
Token string `toml:"token"`
}
func NewConfig() *Config {
return &Config{
MirrorRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
Auth: AuthConfig{Token: "FakeToken"},
CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
Auth: AuthConfig{Token: "FakeToken"},
}
}
@@ -31,7 +31,7 @@ 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)
return nil, fmt.Errorf("error loading config from %s: %w", path, err)
}
if err = cfg.validate(); err != nil {
@@ -42,7 +42,7 @@ func ReadConfig(path string) (*Config, error) {
}
func (c *Config) validate() error {
if c.MirrorRoot == "" {
if c.CacheRoot == "" {
return fmt.Errorf("cache root is required")
}
if c.MirrorURL == "" {
@@ -51,5 +51,8 @@ func (c *Config) validate() error {
if c.Port == "" {
c.Port = "8090"
}
if c.Auth.Token == "" {
return fmt.Errorf("auth token is required")
}
return nil
}