yamlsettings/yamlsettings_test.go

85 lines
2.2 KiB
Go
Raw Permalink Normal View History

2020-07-14 04:39:24 +00:00
package yamlsettings_test
import (
"bytes"
"fmt"
"testing"
"git.kirsle.net/go/yamlsettings"
)
func TestLoadReaders(t *testing.T) {
type Settings struct {
Databases struct {
PrimarySQL struct {
User string `yaml:"user"`
Passwd string `yaml:"passwd"`
Host string `yaml:"host"`
DB string `yaml:"db"`
Compress bool `yaml:"compress"`
Engine string `yaml:"engine"`
} `yaml:"primary_sql"`
Redis struct {
Host string `yaml:"redis_host"`
Port int `yaml:"redis_port"`
} `yaml:"redis"`
} `yaml:"databases"`
AppConfig struct {
Debug bool `yaml:"DEBUG"`
SecretKey string `yaml:"SECRET_KEY"`
} `yaml:"app_config"`
DebugSQL bool `yaml:"debug_sql"`
DebugProfiler bool `yaml:"debug_profiler"`
}
// The defaults.yml file you can commit to version control,
// it's used as the template config that contains all options,
// has sensible defaults where possible or "CHANGE_ME" where not.
var (
defaultsYaml = bytes.NewBuffer([]byte(`---
# Program Defaults, do not edit this file!!!
# All values should be overridden in the settings.yml file.
databases:
primary_sql:
user: my_user
passwd: password_here
host: db-bouncer-01.postgres.com:5432
db: postgres
compress: true
engine: postgresql2
redis:
redis_host: 127.0.0.1
redis_port: 6379
app_config:
DEBUG: false
SECRET_KEY: hard key to guess and keep values secret
debug_sql: false
debug_profiler: false`))
settingsYaml = bytes.NewBuffer([]byte(`---
databases:
primary_sql:
user: root
passwd: god
redis:
redis_host: redis.service.com
app_config:
DEBUG: true
SECRET_KEY: sdfasjksdfASFAS23423f@#$%!$#VR@%UQ%
debug_sql: true`))
)
// Load the defaults and the optional settings file.
// yamlsettings.LoadFiles("defaults.yml", "settings.yml", &config)
var config Settings
yamlsettings.LoadReaders(defaultsYaml, settingsYaml, &config)
fmt.Printf("Redis host: %s\n", config.Databases.Redis.Host)
fmt.Printf("Secret: %s\n", config.AppConfig.SecretKey)
// Load from yaml files on disk.
yamlsettings.LoadFiles("example/defaults.yml", "example/settings.yml", &config)
fmt.Printf("Redis host: %s\n", config.Databases.Redis.Host)
}