2022-04-11 03:59:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPort = 7099
|
|
|
|
defaultInstanceName = "BBJ"
|
|
|
|
defaultHost = "127.0.0.1"
|
2022-04-12 20:33:13 +00:00
|
|
|
defaultDBPath = "db.sqlite3"
|
2022-04-11 03:59:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func parseConfig(configPath string) (*Config, error) {
|
|
|
|
cfgBytes, err := os.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(cfgBytes, &cfg); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Port == 0 {
|
|
|
|
cfg.Port = defaultPort
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.InstanceName == "" {
|
|
|
|
cfg.InstanceName = defaultInstanceName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Host == "" {
|
|
|
|
cfg.Host = defaultHost
|
|
|
|
}
|
|
|
|
|
2022-04-12 20:33:13 +00:00
|
|
|
if cfg.DBPath == "" {
|
|
|
|
cfg.DBPath = defaultDBPath
|
|
|
|
}
|
|
|
|
|
2022-04-11 03:59:26 +00:00
|
|
|
return &cfg, nil
|
|
|
|
}
|