configstore

package module
v0.0.0-...-d4e6dfa Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 13 Imported by: 0

README

ConfigStore

ConfigStore is a go configuration store module.

GoDoc Travis

ConfigStore is initialized by the structure. It converts the structure to the map[string]interface{} as defaults. And then it import environment variables with specified prefix like CFG_, and arguments provided by Cli app.

It is simple as possible. Only a few depends includes:

github.com/BurntSushi/toml

Have fun!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigStore

type ConfigStore struct {
	LayeredStore
	// contains filtered or unexported fields
}

Store is a prioritized configuration registry. It maintains a set of configuration sources, fetches values to populate those, and provides them according to the source's priority.

The priority of the sources is the following:

  1. arguments
  2. env. variables
  3. config file
  4. defaults

For example, if values from the following sources were loaded:

Defaults : {
	"secret": "",
	"user": "default",
	"endpoint": "https://localhost"
}
Config : {
	"user": "root"
	"secret": "defaultsecret"
}
Env : {
	"secret": "somesecretkey"
}

The resulting config will have the following values:

{
	"secret": "somesecretkey",
	"user": "root",
	"endpoint": "https://localhost"
}

func NewConfigStore

func NewConfigStore(dsn string, defaults interface{}) (*ConfigStore, error)

New creates a config by file and struct with default value.

func (*ConfigStore) Arg

func (cs *ConfigStore) Arg() map[string]interface{}

Arg returns the map of arguments.

func (*ConfigStore) Defaults

func (cs *ConfigStore) Defaults() map[string]interface{}

Defaults returns the map cloned from struct.

func (*ConfigStore) Env

func (cs *ConfigStore) Env() map[string]interface{}

Env returns the map of env. variable.

func (*ConfigStore) Load

func (cs *ConfigStore) Load() error

func (*ConfigStore) MapAll

func (cs *ConfigStore) MapAll() map[string]interface{}

MapAll returns the override map includes defaults, file, env. and arguments.

func (*ConfigStore) MapLoaded

func (cs *ConfigStore) MapLoaded() map[string]interface{}

MapLoaded returns the map loaded from file.

func (*ConfigStore) Save

func (cs *ConfigStore) Save(in interface{}) (map[string]interface{}, error)

Save saves the new configure struct.

Return the old configure map and error if occurred.

func (*ConfigStore) SetArg

func (cs *ConfigStore) SetArg(in map[string]interface{})

SetArg sets the map of arguments. It scans and filters all env. variable by reference mas.

func (*ConfigStore) SetEnv

func (cs *ConfigStore) SetEnv(prefixes []string, r *strings.Replacer)

SetEnv sets the map of env. variable. It scans and filters all env. variable by prefix string and reference mas.

type LayeredStore

type LayeredStore interface {

	// Existed returns true if the file was persisted.
	Existed() (bool, error)

	// Remove removes persisted configuration file.
	Remove() error

	// String describes the backing store for the config.
	String() string

	// Close cleans up resources associated with the store.
	Close() error
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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