config

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 10 Imported by: 43

README

go-config

go-config is a configuration loader package.

See godoc.org/github.com/kayac/go-config.

merge-env-config

merge-env-config is the cli tool to deal with template files.

for example:

{
    "name": "some function name",
    "description": "some description",
    "environment": {
        "account_id": "{{ must_env `SOME_MUST_ACCOUNT_ID` }}",
        "secret_key": "{{ env `SOME_SECRET_KEY` }}"
    }
}
$ SOME_MUST_ACCOUNT_ID=must_account_id SOME_SECRET_KEY=some_secret_key merge-env-config -json function.prod.json.tmpl
{
    "name": "some function name",
    "description": "some description",
    "environment": {
        "account_id": "must_account_id",
        "secret_key": "some_secret_key"
    }
}

Author

Copyright (c) 2017 KAYAC Inc.

LICENSE

MIT

Documentation

Overview

Yaml Config Loader

Index

Examples

Constants

This section is empty.

Variables

View Source
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 Load

func Load(conf interface{}, configPaths ...string) error

Load loads YAML files from `configPaths`. and assigns decoded values into the `conf` value.

Example
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)
Output:

&{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example Slave:ro@/example Timeout:500ms}}

func LoadBytes added in v0.1.0

func LoadBytes(conf interface{}, src []byte) error

LoadBytes loads YAML bytes

func LoadJSON

func LoadJSON(conf interface{}, configPaths ...string) error

Load loads JSON files from `configPaths`. and assigns decoded values into the `conf` value.

func LoadJSONBytes added in v0.1.0

func LoadJSONBytes(conf interface{}, src []byte) error

LoadJSONBytes loads JSON bytes

func LoadTOML

func LoadTOML(conf interface{}, configPaths ...string) error

Load loads TOML files from `configPaths`. and assigns decoded values into the `conf` value.

func LoadTOMLBytes added in v0.1.0

func LoadTOMLBytes(conf interface{}, src []byte) error

LoadTOMLBytes loads TOML bytes

func LoadWithEnv

func LoadWithEnv(conf interface{}, configPaths ...string) error

LoadWithEnv loads YAML files with Env replace {{ env "ENV" }} to os.Getenv("ENV") if you set default value then {{ env "ENV" "default" }}

Example
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)
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

func LoadWithEnvBytes(conf interface{}, src []byte) error

LoadWithEnvBytes loads YAML bytes with Env

func LoadWithEnvJSON

func LoadWithEnvJSON(conf interface{}, configPaths ...string) error

LoadWithEnvJSON loads JSON files with Env

func LoadWithEnvJSONBytes added in v0.1.0

func LoadWithEnvJSONBytes(conf interface{}, src []byte) error

LoadWithEnvJSONBytes loads JSON bytes with Env

func LoadWithEnvTOML

func LoadWithEnvTOML(conf interface{}, configPaths ...string) error

LoadWithEnvTOML loads TOML files with Env

func LoadWithEnvTOMLBytes added in v0.1.0

func LoadWithEnvTOMLBytes(conf interface{}, src []byte) error

LoadWithEnvTOMLBytes loads TOML bytes with Env

func MarshalJSON

func MarshalJSON(v interface{}) ([]byte, error)

MarshalJSON returns the JSON encoding of v with indent by 2 white spaces.

Types

This section is empty.

Directories

Path Synopsis
cmd
tfstate module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL