envconf

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2023 License: MIT Imports: 13 Imported by: 5

README

EnvConf

Go Report GoDoc Build and Test codecov

EnvConf is a Go package, for parsing configuration values from different sources.

Installing

go get github.com/antonmashko/envconf

Parse Configs

Usually you need a tag with desire configuration sources and execution of a single function envconf.Parse for getting all configuration values into your golang structure.

Supported configuration sources
  • command line flags
  • environment variables
  • default values
  • external sources (can be anything that is implementing interface External)
Supported tags

Tags:

  • flag - name of flag;
  • env - name of environment variable;
  • default - if nothing set this value will be used as field value;
  • required - on true checks that configuration exists in flag or env source;
  • description - field description in help output.
Example

Let's take a look at a simple example. Here we're creating struct with 3 tags for different configuration sources: flag, env, and default value. NOTE: It's not necessary to specify tags for each configuration type, add desired only

package main

import (
	"fmt"

	"github.com/antonmashko/envconf"
)

type Example struct {
	Field1 string `flag:"flag-name" env:"ENV_VAR_NAME" default:"default-value"`
}

func main() {
	var cfg Example
	if err := envconf.Parse(&cfg); err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", cfg)
}

Testing! If you want to get set Field1 from command line flag, use flag name that is set in flag tag.

$ go run main.go -flag-name="variable-from-flag"
main.Example{Field1:"variable-from-flag"}

The same result would be for other configuration types.

-help output

Using envconf will also generate help output with all registered fields and types. Use flag -help or -h for getting it.

$ go run main.go -help

Usage:

Field1 <string> default-value
        flag: flag-name
        environment variable: ENV_VAR_NAME
        required: false
        description: ""

Configuration Priority

Priority:

1) Flag 
2) Environment variable 
3) External source
4) Default value

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNilData = errors.New("nil data")

ErrNilData mean that exists nil pointer inside data struct

View Source
var (
	ErrUnsupportedType = errors.New("unsupported type")
)
View Source
var FlagParsed func() error

FlagParsed define this callback when you need handle flags This callback will raise after method flag.Parse() return not nil error interrupt pasring

View Source
var UseCustomHelp = true

UseCustomHelp override default flag `Usage`

Functions

func Parse

func Parse(data interface{}) error

Parse define variables inside data from different sources, such as flag/environment variable or default value

func ParseWithExternal

func ParseWithExternal(data interface{}, external External) error

ParseWithExternal works same as Parse method but also can be used external sources (config files, key-value storages, etc.).

func SetLogger

func SetLogger(logger Logger)

SetLogger define debug logger. This logger will print defined values in data fields

func SetPriority

func SetPriority(s ...ConfigSource)

SetPriority overrides default priority order. Default priority order is: Flag, Environment variable, External source, Default value.

Types

type ConfigSource added in v1.1.0

type ConfigSource int
const (
	FlagVariable ConfigSource = iota
	EnvVariable
	ExternalSource
	DefaultValue
)

func (ConfigSource) String added in v1.1.0

func (s ConfigSource) String() string

type EnvConf added in v1.1.0

type EnvConf struct {
	Logger Logger
	// contains filtered or unexported fields
}

func New added in v1.1.0

func New() *EnvConf

func NewWithExternal added in v1.1.0

func NewWithExternal(e External) *EnvConf

func (*EnvConf) Parse added in v1.1.0

func (e *EnvConf) Parse(data interface{}) error

Parse define variables inside data from different sources, such as flag/environment variable or default value

func (*EnvConf) PriorityOrder added in v1.1.0

func (e *EnvConf) PriorityOrder() []ConfigSource

PriorityOrder return parsing priority order

func (*EnvConf) SetPriorityOrder added in v1.1.0

func (e *EnvConf) SetPriorityOrder(s ...ConfigSource)

SetPriorityOrder overrides default priority order. Default priority order is: Flag, Environment variable, External source, Default value.

type Error

type Error struct {
	Inner     error
	Message   string
	FieldName string
}

func (*Error) Error

func (e *Error) Error() string

type External

type External interface {
	// Get string value from values chain(from parent to child)
	Get(...Value) (interface{}, bool)
	//
	Unmarshal(interface{}) error
}

External config source

type JsonConfig

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

func NewJsonConfig

func NewJsonConfig() *JsonConfig

DEPRECATED: Do not use this external type It will be moved to the separate package

func (*JsonConfig) Get

func (j *JsonConfig) Get(values ...Value) (interface{}, bool)

func (*JsonConfig) Read

func (j *JsonConfig) Read(data []byte)

func (*JsonConfig) Unmarshal

func (j *JsonConfig) Unmarshal(v interface{}) error

type Logger

type Logger interface {
	Printf(string, ...interface{})
	Println(...interface{})
}

type Value

type Value interface {
	Owner() Value
	Name() string
	Tag() reflect.StructField
}

DEPRECATED: on next changes this interface will be removed

type Var added in v1.1.0

type Var interface {
	Name() string
	Value() (string, bool)
}

Var is configuration variable for defining primitive data types

Directories

Path Synopsis
external
yaml module

Jump to

Keyboard shortcuts

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