cleanenv

package
v0.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2022 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

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

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

	// Default value
	TagEnvDefault = "env-default"

	// Custom list and map separator
	TagEnvSeparator = "env-separator"

	// Environment variable description
	TagEnvDescription = "env-description"

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

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

	// 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

This section is empty.

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 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 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