Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GlobalLoader ¶
Loader for global config. An instance of this type is usually used by global variables.
If you want to load config files easually, define the following codes in the config directory(e.g. config/config.go):
// Types type Config struct { Hoge string `yaml:"hoge"` } // Files //go:embed *.yaml var Files embed.FS // Envs type Env string const ( EnvTest Env = "test" EnvDev Env = "dev" EnvStg Env = "stg" EnvProd Env = "prod" ) func CurrentEnv() Env { if confutil.IsTestEnv() { return EnvTest } env := os.Getenv("ENV") if env == "" { return EnvDev } return Env(env) } // Loader var globalLoader = confutil.NewGlobalLoader[Config](Files, CurrentEnv) func Load() error { return globalLoader.Load() } func Get() *Config { return globalLoader.Get() }
func NewGlobalLoader ¶
func NewGlobalLoader[C any, E ~string](fs_ fs.FS, getCurrentEnv func() E) *GlobalLoader[C, E]
func (*GlobalLoader[C, E]) Get ¶
func (gl *GlobalLoader[C, E]) Get() *C
func (*GlobalLoader[C, E]) Load ¶
func (gl *GlobalLoader[C, E]) Load() error
type Loader ¶
Load specified YAML files and generate a config instance.
Load the following order, and merge recursively(1. is required, 2. and later are read if the corresponding file exists):
- config.yaml
- config.local.yaml
- config.${env}.yaml
- config.${env}.local.yaml
Each YAML files can use text/template notation (see YAMLTemplate for details):
host: {{ env "DB_HOST" "localhost" }}
Also, common data that can be used in each `config.yaml` files can be described in `data.yaml` and `data.local.yaml`, and templates can be used as well.
The YAML files are read recursively in the following order:
- data.yaml
- data.local.yaml
Those data can be referenced to each `config.yaml` files as follows:
*data.yaml:*
clone_db: host: https://clone-db port: 3306 user: clone_db_user pass: clone_db_pass
*config.yaml:*
db: {{ .clone_db | yaml }}
You can confirm the final configuration value with the `go run ./cmd/config` command:
$ go run ./cmd/config { "DB": { "Host": "localhost", ... }
type YAMLTemplate ¶
type YAMLTemplate struct {
// contains filtered or unexported fields
}
Template engine notated by text/template for YAML files.
func NewYAMLTemplate ¶
func NewYAMLTemplate(data map[string]interface{}) *YAMLTemplate
func (*YAMLTemplate) Env ¶
func (t *YAMLTemplate) Env(key string, defaultValues ...string) string
Get an environment variable specified by `key`. When this value is empty, it returns the first non-zero value specified by `defaultValues`.
os.Setenv("A", "1") a: {{ env "A" }} #=> a: 1 b: {{ env "B" "val" }} #=> b: val c: {{ env "C" (env "D") "val" }} #=> c: val
func (*YAMLTemplate) Str ¶
func (t *YAMLTemplate) Str(value interface{}) string
Transform a specified value to a string value.
data := map[string]interface{}{ "hoge": map[string]interface{}{ "key1": "value1", "key2": "value2", } } a: {{ .hoge.key1 | str }} #=> a: value1 b: {{ .hoge.key3 | str }} #=> b:
func (*YAMLTemplate) Yaml ¶
func (t *YAMLTemplate) Yaml(value interface{}) string
Transform a specified value to a YAML value.
data := map[string]interface{}{ "hoge": map[string]interface{}{ "key1": "value1", "key2": "value2", } } a: {{ .hoge.key1 | yaml }} #=> a: "value1" b: {{ .hoge.key3 | yaml }} #=> b: null c: {{ .hoge | yaml }} #=> c: {"key1":"value1","key2":"value2"}
NOTE: YAML is upward compatible to JSON, so if you output an object to JSON, YAML parser interprets it as YAML map or list.