cleanenv

package
v0.2.5-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagEnv - name of the environment variable or a list of names.
	TagEnv = "env"

	// TagEnvLayout -value parsing layout (for types like time.Time).
	TagEnvLayout = "env-layout"

	// TagEnvDefault - default value.
	TagEnvDefault = "env-default"

	// TagEnvSeparator - custom list and map separator.
	TagEnvSeparator = "env-separator"

	// TagEnvDescription - environment variable description.
	TagEnvDescription = "env-description"

	// TagEnvUpd - flag to mark a field as updatable.
	TagEnvUpd = "env-upd"

	// TagEnvRequired - flag to mark a field as required.
	TagEnvRequired = "env-required"

	// TagEnvPrefix - flag to specify prefix for structure fields.
	TagEnvPrefix = "env-prefix"
)

Supported tags.

View Source
const (
	// DefaultSeparator is a default list and map separator character
	DefaultSeparator = ","
)

Variables

View Source
var (
	ErrorFieldNotProvided       = errors.New("field is required but the value is not provided")
	ErrorWrongTypeOfField       = errors.New("wrong type of field")
	ErrorInvalidMapItem         = errors.New("invalid map item")
	ErrorNotSupportedFileFormat = errors.New("this extation format doesn't supported by the parser")
	ErrorNotSupportedType       = errors.New("this type of field doesn't supported by the parser")
)

Functions

func FUsage

func FUsage(w io.Writer, cfg interface{}, headerText *string, usageFuncs ...func()) func()

FUsage prints configuration help into the custom output. Other usage instructions can be wrapped in and executed before this usage function

func GetDescription

func GetDescription(cfg interface{}, headerText *string) (string, error)

GetDescription returns a description of environment variables. You can provide a custom header text.

func ReadConfig

func ReadConfig(path string, cfg interface{}) error

ReadConfig reads configuration file and parses it depending on tags in structure provided. Then it reads and parses

Example:

type ConfigDatabase struct {
	Port     string `yaml:"port" env:"PORT" env-default:"5432"`
	Host     string `yaml:"host" env:"HOST" env-default:"localhost"`
	Name     string `yaml:"name" env:"NAME" env-default:"postgres"`
	User     string `yaml:"user" env:"USER" env-default:"user"`
	Password string `yaml:"password" env:"PASSWORD"`
}

var cfg ConfigDatabase

err := cleanenv.ReadConfig("config.yml", &cfg)
if err != nil {
    ...
}

func ReadConfigFromReader

func ReadConfigFromReader(reader io.Reader, ext ConfigType, cfg interface{}) error

func ReadEnv

func ReadEnv(cfg interface{}) error

ReadEnv reads environment variables into the structure.

func UpdateEnv

func UpdateEnv(cfg interface{}) error

UpdateEnv rereads (updates) environment variables in the structure.

func Usage

func Usage(cfg interface{}, headerText *string, usageFuncs ...func()) func()

Usage returns a configuration usage help. Other usage instructions can be wrapped in and executed before this usage function. The default output is STDERR.

Types

type ConfigType

type ConfigType int64
const (
	YAML ConfigType = iota
	JSON
	TOML
	EDN
	ENV
	XML
)

type Setter

type Setter interface {
	SetValue(string) error
}

Setter is an interface for a custom value setter.

To implement a custom value setter you need to add a SetValue function to your type that will receive a string raw value:

type MyField string

func (f *MyField) SetValue(s string) error {
	if s == "" {
		return fmt.Errorf("field value can't be empty")
	}
	*f = MyField("my field is: " + s)
	return nil
}

type Updater

type Updater interface {
	Update() error
}

Updater gives an ability to implement custom update function for a field or a whole structure.

Jump to

Keyboard shortcuts

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