hugorm

package module
v1.0.0-pre4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 15 Imported by: 0

README

gone/hugorm

Hugorm is a relatively harmless Viper.

Overview

Hugorm is work-in-progress.

The fundamental motivation for Hugorm is a recognition that the gone/jconf library is hard to extend with further features. With inspiration from github.com/spf13/viper I acknowledged the need for a model where the config is parsed into an intermediate format of map[string]interface{}.

The Viper library also has limitations (like the inability to handle nil values and emptry maps), so instead of waiting for a Viper2 I refactored it all into my preferences. Hugorm is the result.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddConfigFile

func AddConfigFile(format, filename string)

func AddConfigFrom

func AddConfigFrom(format string, in io.Reader) error

AddConfigFrom will parse the data in the provided io.Reader and append it to the config.

func AddConfigMap

func AddConfigMap(cfg map[string]interface{}) error

AddConfigMap appends the configuration from the map given as a constant config source

func AllowEmptyEnv

func AllowEmptyEnv(allowEmptyEnv bool)

AllowEmptyEnv tells Hugorm to consider set, but empty environment variables as valid values instead of falling back. For backward compatibility reasons this is false by default.

func BindEnv

func BindEnv(input ...string) error

BindEnv binds a Hugorm key to a ENV variable. ENV variables are case sensitive. If only a key is provided, it will use the env key matching the key, uppercased. If more arguments are provided, they will represent the env variable names that should bind to this key and will be taken in the specified order. EnvPrefix will be used when set when env name is not provided.

func Config

func Config() map[string]interface{}

Config returns the consolidated config

func Debug

func Debug()

Debug prints all configuration registries for debugging purposes.

func Get

func Get(key string) interface{}

Get can retrieve any value given the key to use. Get is case-insensitive for a key. Get has the behavior of returning the value associated with the first place from where it is set. Viper will check in the following order: override, flag, env, config file, key/value store, default

Get returns an interface. For a specific value use one of the Get____ methods.

func LoadConfig

func LoadConfig() error

LoadConfig will discover and load the configuration file from disk and key/value stores, searching in one of the defined paths.

func Marshal

func Marshal(out io.Writer) error

Marshal (alias for WriteConfigTo)

func Reset

func Reset(opts ...Option)

Reset is intended for testing, will reset all to default settings. In the public interface for the viper package so applications can use it in their testing as well.

func Set

func Set(key string, value interface{})

Set sets the value for the key in the override register. Set is case-insensitive for a key. Will be used instead of values obtained via flags, config file, ENV, default, or key/value store.

func SetDefault

func SetDefault(key string, value interface{})

SetDefault sets the default value for this key. Default only used when no value is provided by the user via flag, config or ENV.

func SetEnvPrefix

func SetEnvPrefix(in string)

SetEnvPrefix defines a prefix that ENVIRONMENT variables will use. E.g. if your prefix is "spf", the env registry will look for env variables that start with "SPF_".

func Unmarshal

func Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error

Unmarshal unmarshals the config into a Struct. Make sure that the tags on the fields of the structure are properly set.

func WriteConfigTo

func WriteConfigTo(out io.Writer) error

WriteConfig writes the current configuration to a file.

Types

type ConfigLoader

type ConfigLoader interface {
	ConfigSource
	Load() error
}

ConfigLoader is a ConfigSource which needs explicit loading to refresh it's values

type ConfigMarshalError

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

ConfigMarshalError happens when failing to marshal the configuration.

func (ConfigMarshalError) Error

func (e ConfigMarshalError) Error() string

Error returns the formatted configuration error.

type ConfigParseError

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

ConfigParseError denotes failing to parse configuration file.

func (ConfigParseError) Error

func (pe ConfigParseError) Error() string

Error returns the formatted configuration error.

type ConfigSource

type ConfigSource interface {
	Values() map[string]interface{}
}

ConfigSource is a case sensitive recursive store of config key/value (values can be maps)

type DecoderConfigOption

type DecoderConfigOption func(*mapstructure.DecoderConfig)

A DecoderConfigOption can be passed to hugorm.Unmarshal to configure mapstructure.DecoderConfig options

func DecodeHook

DecodeHook returns a DecoderConfigOption which overrides the default DecoderConfig.DecodeHook value, the default is:

 mapstructure.ComposeDecodeHookFunc(
		mapstructure.StringToTimeDurationHookFunc(),
		mapstructure.StringToSliceHookFunc(","),
	)

type File

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

func (*File) Load

func (c *File) Load() (err error)

func (*File) Values

func (c *File) Values() map[string]interface{}

type Hugorm

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

Hugorm 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. overrides (see the Set() function) 2. flags 3. env. variables 4. other config sources, - per default a file in a supported format 5. defaults (see the SetDefault() function)

Config sources can be hierarchical (like a JSON file), but each value still has a unique key in a flat keyspace. (using a key-delimiter to define it's path)

So - given a key-delimiter of "." the following will be true:

JSON config:
{
  "foo" : {
     "bar": "baz"
  }
}

key "foo.bar" == "baz"

func GetGlobal

func GetGlobal() *Hugorm

GetGlobal gets the global Hugorm instance.

func New

func New(opts ...Option) *Hugorm

New returns an initialized Viper instance.

func (*Hugorm) AddConfigFile

func (h *Hugorm) AddConfigFile(format, filename string)

func (*Hugorm) AddConfigFrom

func (h *Hugorm) AddConfigFrom(format string, in io.Reader) error

func (*Hugorm) AddConfigMap

func (h *Hugorm) AddConfigMap(cfg map[string]interface{}) error

func (*Hugorm) AllowEmptyEnv

func (h *Hugorm) AllowEmptyEnv(allowEmptyEnv bool)

func (*Hugorm) BindEnv

func (h *Hugorm) BindEnv(input ...string) error

func (*Hugorm) Config

func (h *Hugorm) Config() map[string]interface{}

func (*Hugorm) Debug

func (h *Hugorm) Debug()

func (*Hugorm) Get

func (h *Hugorm) Get(key string) interface{}

func (*Hugorm) LoadConfig

func (h *Hugorm) LoadConfig() error

func (*Hugorm) Marshal

func (h *Hugorm) Marshal(out io.Writer) error

func (*Hugorm) Set

func (h *Hugorm) Set(key string, value interface{})

func (*Hugorm) SetDefault

func (h *Hugorm) SetDefault(key string, value interface{})

func (*Hugorm) SetEnvPrefix

func (h *Hugorm) SetEnvPrefix(in string)

func (*Hugorm) Unmarshal

func (h *Hugorm) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error

func (*Hugorm) WriteConfigTo

func (h *Hugorm) WriteConfigTo(out io.Writer) error

type Option

type Option interface {
	// contains filtered or unexported methods
}

func CaseSensitive

func CaseSensitive(sensitive bool) Option

func ConfigFile

func ConfigFile(format, name string) Option

func EnvPrefix

func EnvPrefix(pfx string) Option

func KeyDelimiter

func KeyDelimiter(d string) Option

KeyDelimiter sets the delimiter used for determining key parts. By default it's value is ".".

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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