feat: Initial
This commit is contained in:
commit
f0abca60db
26 files changed
+1112
No files matched your search
@@ -0,0 +1,62 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type ServiceInfo struct {
|
||||
Name string `yaml:"name" json:"name" bson:"name"`
|
||||
}
|
||||
Reference in new issue
Block a user