Files
homelab/knecht/config/config.go
2026-04-04 15:05:08 +02:00

54 lines
1.1 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
type Config struct {
URL string `toml:"url"`
Token string `toml:"token"`
Endpoint string `toml:"endpoint"` // optional, auto-discovered if empty
ServicesPath string `toml:"services_path"` // optional, defaults to git root / services
Ignore []string `toml:"ignore"`
}
func Load() (*Config, error) {
path, err := configPath()
if err != nil {
return nil, err
}
var cfg Config
if _, err := toml.DecodeFile(path, &cfg); err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("config file not found at %s", path)
}
return nil, fmt.Errorf("parsing config: %w", err)
}
if cfg.URL == "" {
return nil, fmt.Errorf("url is required in %s", path)
}
if cfg.Token == "" {
return nil, fmt.Errorf("token is required in %s", path)
}
return &cfg, nil
}
func Path() (string, error) {
return configPath()
}
func configPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".config", "knecht", "config.toml"), nil
}