envconf

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 11 Imported by: 5

README

EnvConf

Go Report GoDoc Build Status Codecov

Installing

go get github.com/antonmashko/envconf

What is EnvConf?

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

How it works?

  • Import envconf package:
import "github.com/antonmashko/envconf"
  • Create golang struct with following tags (NOTE: each of fields can be omitted, here used all just for example)
type Example struct {
    Field1 string `flag:"field" env:"EXAMPLE_FIELD" default:"example_value" required:"false" description:"this is exaple configurable field"`
}
  • Parse outside values to Exaple struct
func main() {
    var e Example
    envconf.Parse(&e)
    println(e.Field1)
}
  • Testings application with different passing params:

a) flag

./example -field='flag example'
flag example

b) environment variable

EXAMPLE_FIELD='env variable example' ./example
env variable example

c) default value

./example
example_value

How to get all registered fields?

To print all values which can be parsed use flag -help or -h:

./example -help

Usage:

Field1 <string> example_value
        flag: field
        environment variable: EXAMPLE_FIELD
        required: false
        description: "this is exaple configurable field"

Tags description:

Priority:

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

Tags:

  • flag - name of flag for field [must be unique];
  • env - name of environment variable for field [must be unique];
  • default - if nothing set this value will be used;
  • required - on true validate value from flag or env source;
  • description - description in help

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 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 IgnoreNilData = false

IgnoreNilData throw ErrNilData error if pointer(s) inside data struct is nil

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 setted values in data fields

func SetPriority

func SetPriority(priority ...Priority)

SetPriority can override default priority queue. Default priority queue is: Flag, Environment variable, External source, Default value.

Types

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

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 Priority

type Priority int
const (
	FlagPriority Priority = iota
	EnvPriority
	ExternalPriority
	DefaultPriority
)

func (Priority) String

func (p Priority) String() string

type Value

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

Directories

Path Synopsis
external
yaml module

Jump to

Keyboard shortcuts

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