Documentation
¶
Overview ¶
Yaml Config Loader
Index ¶
- Variables
- func Delims(left, right string)
- func Funcs(funcMap template.FuncMap)
- func Load(conf interface{}, configPaths ...string) error
- func LoadBytes(conf interface{}, src []byte) error
- func LoadJSON(conf interface{}, configPaths ...string) error
- func LoadJSONBytes(conf interface{}, src []byte) error
- func LoadTOML(conf interface{}, configPaths ...string) error
- func LoadTOMLBytes(conf interface{}, src []byte) error
- func LoadWithEnv(conf interface{}, configPaths ...string) error
- func LoadWithEnvBytes(conf interface{}, src []byte) error
- func LoadWithEnvJSON(conf interface{}, configPaths ...string) error
- func LoadWithEnvJSONBytes(conf interface{}, src []byte) error
- func LoadWithEnvTOML(conf interface{}, configPaths ...string) error
- func LoadWithEnvTOMLBytes(conf interface{}, src []byte) error
- func MarshalJSON(v interface{}) ([]byte, error)
- func ReadWithEnv(configPath string) ([]byte, error)
- func ReadWithEnvBytes(b []byte) ([]byte, error)
- type Loader
- func (l *Loader) Delims(left, right string)
- func (l *Loader) Funcs(funcMap template.FuncMap)
- func (l *Loader) Load(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadBytes(conf interface{}, src []byte) error
- func (l *Loader) LoadJSON(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadJSONBytes(conf interface{}, src []byte) error
- func (l *Loader) LoadTOML(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadTOMLBytes(conf interface{}, src []byte) error
- func (l *Loader) LoadWithEnv(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadWithEnvBytes(conf interface{}, src []byte) error
- func (l *Loader) LoadWithEnvJSON(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadWithEnvJSONBytes(conf interface{}, src []byte) error
- func (l *Loader) LoadWithEnvTOML(conf interface{}, configPaths ...string) error
- func (l *Loader) LoadWithEnvTOMLBytes(conf interface{}, src []byte) error
- func (l *Loader) ReadWithEnv(configPath string) ([]byte, error)
- func (l *Loader) ReadWithEnvBytes(b []byte) ([]byte, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultFuncMap = template.FuncMap{ "env": func(keys ...string) string { v := "" for _, k := range keys { v = os.Getenv(k) if v != "" { return v } v = k } return v }, "must_env": func(key string) string { if v, ok := os.LookupEnv(key); ok { return v } panic(fmt.Sprintf("environment variable %s is not defined", key)) }, "json_escape": func(s string) string { b, _ := json.Marshal(s) return string(b[1 : len(b)-1]) }, }
DefaultFuncMap defines built-in template functions.
var Marshal = yaml.Marshal
Marshal serializes the value provided into a YAML document.
Functions ¶
func Delims ¶ added in v0.2.0
func Delims(left, right string)
Delims sets the action delimiters to the specified strings.
func Funcs ¶ added in v0.3.0
Funcs adds the elements of the argument map. Caution: global settings are overwritten. can't go back.
func Load ¶
Load loads YAML files from `configPaths`. and assigns decoded values into the `conf` value.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/kayac/go-config"
)
var dir string
func main() {
type DBConfig struct {
Master string `yaml:"master"`
Slave string `yaml:"slave"`
Timeout time.Duration `yml:"timeout"`
}
type Conf struct {
Domain string `yaml:"domain"`
IsDev bool `yaml:"is_dev"`
Timeout time.Duration `yml:"timeout"`
DB DBConfig `yaml:"db"`
}
baseConfig := `
# config.yml
domain: example.com
db:
master: rw@/example
slave: ro@/example
timeout: 0.5s
`
localConfig := `
# config_local.yml
domain: dev.example.com
is_dev: true
`
conf := &Conf{}
baseConfigYaml, _ := genConfigFile("config.yml", baseConfig) // /path/to/config.yml
localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml
err := config.Load(conf, baseConfigYaml, localConfigYaml)
if err != nil {
panic(err)
}
fmt.Printf("%+v", conf)
}
func genConfigFile(name string, config string) (string, error) {
path := filepath.Join(dir, name)
io, err := os.Create(path)
if err != nil {
return "", err
}
if _, err := io.WriteString(config); err != nil {
return "", err
}
return path, nil
}
Output: &{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example Slave:ro@/example Timeout:500ms}}
func LoadJSON ¶
Load loads JSON files from `configPaths`. and assigns decoded values into the `conf` value.
func LoadJSONBytes ¶ added in v0.1.0
LoadJSONBytes loads JSON bytes
func LoadTOML ¶
Load loads TOML files from `configPaths`. and assigns decoded values into the `conf` value.
func LoadTOMLBytes ¶ added in v0.1.0
LoadTOMLBytes loads TOML bytes
func LoadWithEnv ¶
LoadWithEnv loads YAML files with Env replace {{ env "ENV" }} to os.Getenv("ENV") if you set default value then {{ env "ENV" "default" }}
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/kayac/go-config"
)
var dir string
func main() {
type DBConfig struct {
Master string `yaml:"master"`
Slave string `yaml:"slave"`
Timeout time.Duration `yml:"timeout"`
}
type Conf struct {
Domain string `yaml:"domain"`
IsDev bool `yaml:"is_dev"`
Timeout time.Duration `yml:"timeout"`
DB DBConfig `yaml:"db"`
}
baseConfig := `
# config.yml
domain: {{ env "DOMAIN"}}
db:
master: rw@/example
slave: ro@/example
timeout: 0.5s
`
localConfig := `
# config_local.yml
is_dev: true
db:
master: {{ env "RW_USER" "rw" }}@/{{ env "DB_NAME" "example_dev" }}
slave: {{ env "RO_USER" "ro" }}@/{{ env "DB_NAME" "example_dev" }}
`
os.Setenv("DOMAIN", "dev.example.com")
os.Setenv("DB_NAME", "example_local")
conf := &Conf{}
baseConfigYaml, _ := genConfigFile("config.yml", baseConfig) // /path/to/config.yml
localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml
err := config.LoadWithEnv(conf, baseConfigYaml, localConfigYaml)
if err != nil {
panic(err)
}
fmt.Printf("%+v", conf)
}
func genConfigFile(name string, config string) (string, error) {
path := filepath.Join(dir, name)
io, err := os.Create(path)
if err != nil {
return "", err
}
if _, err := io.WriteString(config); err != nil {
return "", err
}
return path, nil
}
Output: &{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example_local Slave:ro@/example_local Timeout:500ms}}
func LoadWithEnvBytes ¶ added in v0.1.0
LoadWithEnvBytes loads YAML bytes with Env
func LoadWithEnvJSON ¶
LoadWithEnvJSON loads JSON files with Env
func LoadWithEnvJSONBytes ¶ added in v0.1.0
LoadWithEnvJSONBytes loads JSON bytes with Env
func LoadWithEnvTOML ¶
LoadWithEnvTOML loads TOML files with Env
func LoadWithEnvTOMLBytes ¶ added in v0.1.0
LoadWithEnvTOMLBytes loads TOML bytes with Env
func MarshalJSON ¶
MarshalJSON returns the JSON encoding of v with indent by 2 white spaces.
func ReadWithEnv ¶ added in v0.6.0
func ReadWithEnvBytes ¶ added in v0.6.0
Types ¶
type Loader ¶ added in v0.3.0
type Loader struct {
Data interface{}
// contains filtered or unexported fields
}
Loader represents config loader.
func (*Loader) Load ¶ added in v0.3.0
Load loads YAML files from `configPaths`. and assigns decoded values into the `conf` value.
func (*Loader) LoadJSON ¶ added in v0.3.0
LoadJSON loads JSON files from `configPaths`. and assigns decoded values into the `conf` value.
func (*Loader) LoadJSONBytes ¶ added in v0.3.0
LoadJSONBytes loads JSON bytes
func (*Loader) LoadTOML ¶ added in v0.3.0
LoadTOML loads TOML files from `configPaths`. and assigns decoded values into the `conf` value.
func (*Loader) LoadTOMLBytes ¶ added in v0.3.0
LoadTOMLBytes loads TOML bytes
func (*Loader) LoadWithEnv ¶ added in v0.3.0
LoadWithEnv loads YAML files with Env replace {{ env "ENV" }} to os.Getenv("ENV") if you set default value then {{ env "ENV" "default" }}
func (*Loader) LoadWithEnvBytes ¶ added in v0.3.0
LoadWithEnvBytes loads YAML bytes with Env
func (*Loader) LoadWithEnvJSON ¶ added in v0.3.0
LoadWithEnvJSON loads JSON files with Env
func (*Loader) LoadWithEnvJSONBytes ¶ added in v0.3.0
LoadWithEnvJSONBytes loads JSON bytes with Env
func (*Loader) LoadWithEnvTOML ¶ added in v0.3.0
LoadWithEnvTOML loads TOML files with Env
func (*Loader) LoadWithEnvTOMLBytes ¶ added in v0.3.0
LoadWithEnvTOMLBytes loads TOML bytes with Env