env

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: Apache-2.0 Imports: 12 Imported by: 8

README

env

GoDoc Build Status

Implements a simple way of handling environment values. Each environment field is simply reflected by a variable inside the Go program. Out of the box handlers for the types bool, []byte, time.Duration, int, []int, string and []string are provided. Other types can be added by using the RegisterField function.

Example

var (
	name = env.String("NAME", "joe")
	age  = env.Int("AGE", 24)
)

func main() {
	env.ParseFlags()

	fmt.Printf("%s is %d years old\n", name.Get(), age.Get())
}

If the program is called with -print short-bash, all registered environment fields would be printed...

NAME="joe"
AGE="24"

By using -print long-bash, a description for each field is generated.

# String field. The default value is 'joe'.
NAME="joe"

# Int field. The default value is '24'.
AGE="24"

License

The project is licensed under Apache 2.0.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NullErrorHandler   = func(error) {}
	StdoutErrorHandler = func(err error) {
		fmt.Fprintln(os.Stdout, err)
	}
	StderrErrorHandler = func(err error) {
		fmt.Fprintln(os.Stderr, err)
	}
	LogErrorHandler = func(err error) {
		log.Println(err)
	}
)

Implementations of different error handlers.

View Source
var ErrorHandler = StderrErrorHandler

ErrorHandler defines a handler for error messages. By default, LogErrorHandler is set.

Functions

func Clear

func Clear()

Clear clears the field register.

func Fields

func Fields() []string

Fields returns a slice of strings with all registered fields.

func ParseFlags

func ParseFlags()

ParseFlags tests if the print-flag was given at the program start and prints the registered environment fields with thier values to stdout using the specified format. Afterwards, the program exits with return code 2.

func Print

func Print(w io.Writer, format string) error

Print prints the environment in the provided format.

Types

type BoolField

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

BoolField implements a duration field.

func Bool

func Bool(name string, defaultValue bool, opts ...Option) *BoolField

Bool registers a field of the provided name.

func (*BoolField) DefaultValue

func (i *BoolField) DefaultValue() string

DefaultValue returns the field's default value.

func (*BoolField) Description

func (i *BoolField) Description() string

Description returns the field's description.

func (*BoolField) Get

func (i *BoolField) Get() bool

Get returns the field value or the default value.

func (BoolField) Name

func (f BoolField) Name() string

func (*BoolField) Value

func (i *BoolField) Value() string

Value returns the field's value.

type BytesField

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

BytesField implements a string field.

func Bytes

func Bytes(name string, defaultValue []byte, opts ...Option) *BytesField

Bytes registers a field of the provided name.

func (*BytesField) DefaultValue

func (bf *BytesField) DefaultValue() string

DefaultValue returns the field's default value.

func (*BytesField) Description

func (bf *BytesField) Description() string

Description returns the field's description.

func (*BytesField) Get

func (bf *BytesField) Get() []byte

Get returns the field value or the default value.

func (BytesField) Name

func (f BytesField) Name() string

func (*BytesField) Value

func (bf *BytesField) Value() string

Value returns the field's value.

type DurationField

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

DurationField implements a duration field.

func Duration

func Duration(name string, defaultValue time.Duration, opts ...Option) *DurationField

Duration registers a field of the provided name.

func (*DurationField) DefaultValue

func (df *DurationField) DefaultValue() string

DefaultValue returns the field's default value.

func (*DurationField) Description

func (df *DurationField) Description() string

Description returns the field's description.

func (*DurationField) Get

func (df *DurationField) Get() time.Duration

Get returns the field value or the default value.

func (DurationField) Name

func (f DurationField) Name() string

func (*DurationField) Value

func (df *DurationField) Value() string

Value returns the field's value.

type Field

type Field interface {
	Name() string
	Value() string
	DefaultValue() string
	Description() string
}

Field implements an environment configuration field.

func RegisterField

func RegisterField(field Field) Field

RegisterField adds the provided `Field` to the global field-register.

type IntField

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

IntField implements a duration field.

func Int

func Int(name string, defaultValue int, opts ...Option) *IntField

Int registers a field of the provided name.

func (*IntField) DefaultValue

func (i *IntField) DefaultValue() string

DefaultValue returns the field's default value.

func (*IntField) Description

func (i *IntField) Description() string

Description returns the field's description.

func (*IntField) Get

func (i *IntField) Get() int

Get returns the field value or the default value.

func (IntField) Name

func (f IntField) Name() string

func (*IntField) Value

func (i *IntField) Value() string

Value returns the field's value.

type IntsField

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

IntsField implements a ints field.

func Ints

func Ints(name string, defaultValue []int, opts ...Option) *IntsField

Ints registers a field of the provided name.

func (*IntsField) DefaultValue

func (isf *IntsField) DefaultValue() string

DefaultValue returns the field's default value.

func (*IntsField) Description

func (isf *IntsField) Description() string

Description returns the field's description.

func (*IntsField) Get

func (isf *IntsField) Get() []int

Get returns the field value or the default value.

func (IntsField) Name

func (f IntsField) Name() string

func (*IntsField) Value

func (isf *IntsField) Value() string

Value returns the field's value.

type Option

type Option func(*options)

Option defines an Option that can modify the options struct.

func AllowedValues

func AllowedValues(values ...string) Option

AllowedValues returns an Option that defines all allowed values for the environment field.

func Description

func Description(text string) Option

Description returns an Option that sets the description of the environment field.

func Required

func Required() Option

Required returns an Option that makes the environment field required.

type StringField

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

StringField implements a string field.

func String

func String(name, defaultValue string, opts ...Option) *StringField

String registers a field of the provided name.

func (*StringField) DefaultValue

func (sf *StringField) DefaultValue() string

DefaultValue returns the field's default value.

func (*StringField) Description

func (sf *StringField) Description() string

Description returns the field's description.

func (*StringField) Get

func (sf *StringField) Get() string

Get returns the field value or the default value.

func (StringField) Name

func (f StringField) Name() string

func (*StringField) Value

func (sf *StringField) Value() string

Value returns the field's value.

type StringsField

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

StringsField implements a strings field.

func Strings

func Strings(name string, defaultValue []string, opts ...Option) *StringsField

Strings registers a field of the provided name.

func (*StringsField) DefaultValue

func (sf *StringsField) DefaultValue() string

DefaultValue returns the field's default value.

func (*StringsField) Description

func (sf *StringsField) Description() string

Description returns the field's description.

func (*StringsField) Get

func (sf *StringsField) Get() []string

Get returns the field value or the default value.

func (StringsField) Name

func (f StringsField) Name() string

func (*StringsField) Value

func (sf *StringsField) Value() string

Value returns the field's value.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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