package config import ( "errors" "gopkg.in/yaml.v3" "os" "path/filepath" ) type UniverseConfig struct { ServiceInfo *ServiceInfo `yaml:"service_info" json:"service_info" bson:"service_info"` Modules map[string]map[string]interface{} `yaml:"modules" json:"modules" bson:"modules"` } var ( wellKnownConfigPlaces []string = []string{ "config.yaml", "config.yml", } ) func LoadConfigFromDirectory[T any](dir string, out *T) error { for _, fileName := range wellKnownConfigPlaces { path := filepath.Join(dir, fileName) if _, err := os.Stat(path); os.IsNotExist(err) { continue } return LoadConfigFromPath(path, out) } return nil } func LoadConfigFromPath[T any](path string, out *T) error { ext := filepath.Ext(path) switch ext { case ".yaml", ".yml": // Load YAML config if err := LoadYamlConfigFromPath(path, out); err != nil { return err } return nil default: return errors.New("Universe config file must be .yaml or .yml") } } func LoadYamlConfigFromPath[T any](path string, out *T) error { file, err := os.Open(path) if err != nil { return err } defer file.Close() decoder := yaml.NewDecoder(file) if err := decoder.Decode(out); err != nil { return err } return nil }