config

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 10 Imported by: 41

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 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.

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 Funcs added in v0.3.0

func Funcs(funcMap template.FuncMap)

Funcs adds the elements of the argument map. Caution: global settings are overwritten. can't go back.

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.

func ReadWithEnv added in v0.6.0

func ReadWithEnv(configPath string) ([]byte, error)

func ReadWithEnvBytes added in v0.6.0

func ReadWithEnvBytes(b []byte) ([]byte, error)

Types

type Loader added in v0.3.0

type Loader struct {
	Data interface{}
	// contains filtered or unexported fields
}

Loader represents config loader.

func New added in v0.3.0

func New() *Loader

New creates a Loader instance.

func (*Loader) Delims added in v0.3.0

func (l *Loader) Delims(left, right string)

Delims sets the action delimiters to the specified strings.

func (*Loader) Funcs added in v0.3.0

func (l *Loader) Funcs(funcMap template.FuncMap)

Funcs adds the elements of the argument map.

func (*Loader) Load added in v0.3.0

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

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

func (*Loader) LoadBytes added in v0.3.0

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

LoadBytes loads YAML bytes

func (*Loader) LoadJSON added in v0.3.0

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

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

func (*Loader) LoadJSONBytes added in v0.3.0

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

LoadJSONBytes loads JSON bytes

func (*Loader) LoadTOML added in v0.3.0

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

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

func (*Loader) LoadTOMLBytes added in v0.3.0

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

LoadTOMLBytes loads TOML bytes

func (*Loader) LoadWithEnv added in v0.3.0

func (l *Loader) 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" }}

func (*Loader) LoadWithEnvBytes added in v0.3.0

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

LoadWithEnvBytes loads YAML bytes with Env

func (*Loader) LoadWithEnvJSON added in v0.3.0

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

LoadWithEnvJSON loads JSON files with Env

func (*Loader) LoadWithEnvJSONBytes added in v0.3.0

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

LoadWithEnvJSONBytes loads JSON bytes with Env

func (*Loader) LoadWithEnvTOML added in v0.3.0

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

LoadWithEnvTOML loads TOML files with Env

func (*Loader) LoadWithEnvTOMLBytes added in v0.3.0

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

LoadWithEnvTOMLBytes loads TOML bytes with Env

func (*Loader) ReadWithEnv added in v0.6.0

func (l *Loader) ReadWithEnv(configPath string) ([]byte, error)

func (*Loader) ReadWithEnvBytes added in v0.6.0

func (l *Loader) ReadWithEnvBytes(b []byte) ([]byte, error)

Directories

Path Synopsis
cmd
tfstate module

Jump to

Keyboard shortcuts

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