confutil

package
v0.0.0-...-70692ee Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTestEnv

func IsTestEnv() bool

Get whether to run `go test`

See: https://qiita.com/chimatter/items/6922e7bd34483a9108b1

func Output

func Output[T any](writer io.Writer, conf *T, isPretty bool) error

Output specified config to JSON

Types

type GlobalLoader

type GlobalLoader[C any, E ~string] struct {
	// contains filtered or unexported fields
}

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

type Loader[C any, E ~string] struct {
	// contains filtered or unexported fields
}

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

  1. config.yaml
  2. config.local.yaml
  3. config.${env}.yaml
  4. 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:

  1. data.yaml
  2. 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",
        ...
}

func NewLoader

func NewLoader[C any, E ~string](fs_ fs.FS) *Loader[C, E]

func (*Loader[C, E]) Load

func (l *Loader[C, E]) Load(env E) (*C, error)

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

func (t *YAMLTemplate) Compile(name string, r io.Reader, w io.Writer) error

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.

Jump to

Keyboard shortcuts

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