config

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 10 Imported by: 1

README

config

Usage

Configuration

type MyConfig struct {
    Host string               `config:"hostname,secret,required"`
    User string               `config:"user,secret,required"`
    Password string           `config:"user,secret,required"`
    ConnTimeout time.Duration `config:"conn_timeout"`
}

Loading

manager := NewManager()

plainEngine := NewYAMLEngine(NewConfigLoader(".config.yaml"))
secretEngine := NewYAMLEngine(NewConfigLoader(".config-secrets.yaml"))

manager.AddPlainEngine(mapEngine)
manager.AddSecretEngine(mapEngine)

var cfg MyConfig
err := manager.Populate(&cfg)
if err != nil {
    panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when a key is not found by an engine.
	ErrKeyNotFound = errors.New("key not found")

	// ErrTypeMismatch is returned when a type mismatch for Engine get operations.
	ErrTypeMismatch = errors.New("type mismatch")

	// ErrNoSecretEngineDefined is returned by Manager.Populate when a secret config is defined but there is no secret
	// engine defined.
	ErrNoSecretEngineDefined = errors.New("no secret engine defined")

	// ErrNoPlainEngineDefined is returned by Manager.Populate when a plain config is defined but there is no plain
	// engine defined.
	ErrNoPlainEngineDefined = errors.New("no plain engine defined")

	// ErrEngineNotLoaded is returned when trying to get a key from an Engine that is not loaded.
	ErrEngineNotLoaded = errors.New("engine not loaded")

	// ErrConfigNotPointer is returned by Manager.Populate when the config is not a pointer.
	ErrConfigNotPointer = errors.New("config not pointer")
)

Functions

This section is empty.

Types

type BytesLoader

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

BytesLoader receives a byte slice and implements the Loader interface.

func NewBytesLoader

func NewBytesLoader(bytes []byte) *BytesLoader

NewBytesLoader creates a new BytesLoader.

func (*BytesLoader) Load

func (loader *BytesLoader) Load() (io.Reader, error)

Load returns a io.Reader from the given bytes (check NewBytesLoader).

func (*BytesLoader) Unload

func (loader *BytesLoader) Unload() error

Unload sets the internal pointer to the given slice to nil.

type Engine

type Engine interface {
	Load() error
	Unload() error

	GetString(key string) (string, error)
	GetStringSlice(key string) ([]string, error)

	GetInt(key string) (int, error)
	GetIntSlice(key string) ([]int, error)

	GetUint(key string) (uint, error)
	GetUintSlice(key string) ([]uint, error)

	GetInt64(key string) (int64, error)
	GetInt64Slice(key string) ([]int64, error)

	GetUint64(key string) (uint64, error)
	GetUint64Slice(key string) ([]uint64, error)

	GetBool(key string) (bool, error)
	GetBoolSlice(key string) ([]bool, error)

	GetFloat(key string) (float64, error)
	GetFloatSlice(key string) ([]float64, error)

	GetDuration(key string) (time.Duration, error)
}

Engine is an interface that provides the contract for configuration engines to be able to read configuration from a variety of sources.

type FileLoader

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

FileLoader is a config loader that loads a file. This can be used to load JSON, YAML, or INI files, according with the Engine that is being used.

func NewFileLoader

func NewFileLoader(filePath string) *FileLoader

func (*FileLoader) Load

func (loader *FileLoader) Load() (io.Reader, error)

Load loads the given filePath (check NewFileLoader) saving the file handler for further use.

func (*FileLoader) Unload

func (loader *FileLoader) Unload() error

Unload closes the file handler.

type Loader

type Loader interface {
	// Load returns an io.Reader for reading the data to be unmarshaled by the
	// Engine implementation.
	Load() (io.Reader, error)
	// Unload is called when the configuration is no longer needed and all the
	// resources should be released.
	Unload() error
}

Loader is the interface for loading configuration from a source.

type Manager

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

func NewManager

func NewManager(opts ...Option) *Manager

func (*Manager) AddPlainEngine

func (m *Manager) AddPlainEngine(engine Engine)

func (*Manager) AddSecretEngine

func (m *Manager) AddSecretEngine(engine Engine)

func (*Manager) Populate

func (m *Manager) Populate(cfg interface{}) error

type MapEngine

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

func NewMapEngine

func NewMapEngine(data map[string]interface{}) *MapEngine

NewMapEngine returns a new instance of MapEngine with the given data.

Internally, it will flatten the data map before storing for future use.

func (*MapEngine) GetBool

func (engine *MapEngine) GetBool(key string) (bool, error)

func (*MapEngine) GetBoolSlice

func (engine *MapEngine) GetBoolSlice(key string) ([]bool, error)

func (*MapEngine) GetDuration

func (engine *MapEngine) GetDuration(key string) (time.Duration, error)

func (*MapEngine) GetFloat

func (engine *MapEngine) GetFloat(key string) (float64, error)

func (*MapEngine) GetFloatSlice

func (engine *MapEngine) GetFloatSlice(key string) ([]float64, error)

func (*MapEngine) GetInt

func (engine *MapEngine) GetInt(key string) (int, error)

func (*MapEngine) GetInt64

func (engine *MapEngine) GetInt64(key string) (int64, error)

func (*MapEngine) GetInt64Slice

func (engine *MapEngine) GetInt64Slice(key string) ([]int64, error)

func (*MapEngine) GetIntSlice

func (engine *MapEngine) GetIntSlice(key string) ([]int, error)

func (*MapEngine) GetString

func (engine *MapEngine) GetString(key string) (string, error)

func (*MapEngine) GetStringSlice

func (engine *MapEngine) GetStringSlice(key string) ([]string, error)

func (*MapEngine) GetUint

func (engine *MapEngine) GetUint(key string) (uint, error)

func (*MapEngine) GetUint64

func (engine *MapEngine) GetUint64(key string) (uint64, error)

func (*MapEngine) GetUint64Slice

func (engine *MapEngine) GetUint64Slice(key string) ([]uint64, error)

func (*MapEngine) GetUintSlice

func (engine *MapEngine) GetUintSlice(key string) ([]uint, error)

func (*MapEngine) Load

func (engine *MapEngine) Load() error

func (*MapEngine) Unload

func (engine *MapEngine) Unload() error

type Option

type Option func(*Manager)

func WithKeySeparator

func WithKeySeparator(separator string) Option

WithKeySeparator sets the key separator for the manager.

type Validator

type Validator interface {
	Validate() error
}

type YAMLEngine

type YAMLEngine struct {
	*MapEngine
	// contains filtered or unexported fields
}

func NewYAMLEngine

func NewYAMLEngine(loader Loader) *YAMLEngine

func (*YAMLEngine) Load

func (engine *YAMLEngine) Load() error

Load loads the YAML file defined by the filePath set on the NewYAMLEngine saving the data into a internal map.

Jump to

Keyboard shortcuts

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