config

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 4, 2018 License: MIT Imports: 14 Imported by: 0

README

Config

Yaml configuration with template, inspired by Ansible and Viper

Usage

  • TODO

Specification

  • using golang's template syntax, not ansible, not pongo2
  • only support single file, but can have multiple documents separated by ---
  • environment variables
    • use env function, i.e. home: {{ env "HOME"}}
  • variables
    • special top level key vars is used via var function
    • vars in different documents are merged, while for other top level keys, their value is replaced by latter documents
    • vars in current document can be used in current document, we actually render the template of each document twice, so in vars section you can use previous vars and all the syntax supported by golang's template
  • condition (not tested, but golang template should support it)
  • loop
    • use the range syntax

Using variables

  • TODO: will xkb --target={{ $name }} --port={{ $db.port }} have undesired blank and/or new line?
vars:
    influxdb_port: 8080
    databases:
        - influxdb
        - kairosdb
influxdb:
    port: {{ var "influxdb_port" }}
kairosdb:
    port: {{ env "KAIROSDB_PORT" }}
tests:
{{ range $name := var "databases" -}}
{{ $db := var $name -}}
    - xkb --target={{ $name }} --port={{ $db.port }}
{{ end }
base: {{ env "HOME" }}

Using multiple document

  • TODO: example
  • TODO: the example in runner actually does not require any template features

YAML parsing problem

  • multiple document support, the author said it is easy, but he never tried to solve them,
    • the encoder & decoder PR seems to support multiple documents but I don't understand why it does.
    • Actually it would be interesting to look into how to unmarshal stream data since that is needed for Xephon-B, though for most formats, go already have encoder and decoder
    • (Adopted) A easier way is to split the whole file by --- before put it into go-yaml

Acknowledgement

Documentation

Overview

Package config supports go text/template, environment and self defined variables

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadYAMLAsStruct

func LoadYAMLAsStruct(file string, structuredConfig interface{}) error

LoadYAMLAsStruct is a convenient wrapper for loading a single YAML file into struct, you should pass a pointer to the struct as second argument. It will remove the vars section.

func LoadYAMLDirect added in v0.0.6

func LoadYAMLDirect(file string, cfg interface{}) error

func LoadYAMLDirectFrom added in v0.0.6

func LoadYAMLDirectFrom(r io.Reader, cfg interface{}) error

func LoadYAMLDirectFromStrict added in v0.0.6

func LoadYAMLDirectFromStrict(r io.Reader, cfg interface{}) error

func LoadYAMLDirectStrict added in v0.0.6

func LoadYAMLDirectStrict(file string, cfg interface{}) error

func SplitMultiDocument

func SplitMultiDocument(data []byte) [][]byte

SplitMultiDocument splits a yaml file that contains multiple documents and (only) trim the first one if it is empty

Types

type Path

type Path string

type Reader

type Reader interface {
	Path() Path
	Content() string
}

type StructuredConfig

type StructuredConfig interface {
	Validate() error
}

type YAMLConfig

type YAMLConfig struct {
	// contains filtered or unexported fields
}

YAMLConfig is a thread safe struct for parse YAML file and get value

func NewYAMLConfig

func NewYAMLConfig() *YAMLConfig

NewYAMLConfig returns a config with internal map structure initialized

func (*YAMLConfig) Env

func (c *YAMLConfig) Env(name string) string

func (*YAMLConfig) EnvOr

func (c *YAMLConfig) EnvOr(name string, defaultVal string) string

func (*YAMLConfig) FuncMaps

func (c *YAMLConfig) FuncMaps() template.FuncMap

func (*YAMLConfig) Get

func (c *YAMLConfig) Get(key string) interface{}

func (*YAMLConfig) GetLogger

func (c *YAMLConfig) GetLogger() *dlog.Logger

func (*YAMLConfig) GetOrDefault

func (c *YAMLConfig) GetOrDefault(key string, defaultVal interface{}) interface{}

func (*YAMLConfig) GetOrFail

func (c *YAMLConfig) GetOrFail(key string) (interface{}, error)

func (*YAMLConfig) LoggerIdentity

func (c *YAMLConfig) LoggerIdentity(justCallMe func() *dlog.Identity) *dlog.Identity

func (*YAMLConfig) ParseMultiDocument

func (c *YAMLConfig) ParseMultiDocument(data []byte) error

func (*YAMLConfig) ParseSingleDocument

func (c *YAMLConfig) ParseSingleDocument(doc []byte) error

func (*YAMLConfig) SetLogger

func (c *YAMLConfig) SetLogger(logger *dlog.Logger)

func (*YAMLConfig) Unmarshal

func (c *YAMLConfig) Unmarshal(structuredConfig interface{}, removeVars bool) error

func (*YAMLConfig) UnmarshalKey

func (c *YAMLConfig) UnmarshalKey(key string, structuredConfig interface{}) error

func (*YAMLConfig) Var

func (c *YAMLConfig) Var(name string) interface{}

Jump to

Keyboard shortcuts

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