appcfg

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 12 Imported by: 4

README

Get valid application configuration from flags/env/config files/consul/… for Go

Go Reference CI/CD Coverage Status Go Report Card Release

Documentation

Overview

Package appcfg helps implement Clean Architecture adapter for application configuration.

Application configuration values may come from different sources (defaults, flags, env, config files, services like consul, etc.). These sources are external for your application and use their own data formats. Same value may comes from several sources in some priority order, in some cases some sources may not be available (like flags in tests, but tests still may needs access to configuration values), etc. At same time some configuration value (like "port" or "timeout") has same format and constraints in your application no matter from which source it comes.

Because of all that complexity it makes sense to handle configuration in same way as any other external data and implement adapter (in terms of Clean Architecture) which will convert configuration received from external sources into data structure convenient for your application.

For this we'll need to define two data structures (first suitable for receiving values from external sources, second suitable for your application) and a conversion logic to create second using values from first one.

First data structure should be able to apply same constraints to some value no matter from which source it comes from. Luckily, we already have suitable interface for this task: flag.Value and the likes. Using compatible interface is a requirement to be able to attach flags to these values, but as same time this interface allows us to accept values as plain strings, which makes it general enough for any sources.

This package provides both a lot of types of Value interface (just like flag and other similar packages like pflag) to be used in first structure, and also some functions to help loading data from different sources (like environment variables) into such Value typed values.

Provided Value interface has more strict semantics than flag.Value, to be able to distinguish between zero and unset values - as it is important to know is required configuration value was provided or not.

See example to see how to use this package, and also check https://github.com/powerman/appcfg/tree/master/examples to see how to test such configuration.

Example
package main

import (
	"flag"
	"log"
	"time"

	"github.com/powerman/appcfg"
)

// Intermediate config, used to gather and validate values from different
// external sources (environment variables, flags, etc.).
var extCfg = struct { // Type defines constraint types and providers for each exported field.
	Host      appcfg.NotEmptyString  `env:"HOST"`
	Port      appcfg.Port            `env:"PORT"`
	BindPorts appcfg.ListenPortSlice `env:"BIND_PORTS"`
	Timeout   appcfg.Duration
	Retries   appcfg.IntBetween `env:"RETRIES"`
	fs        *flag.FlagSet     // Needed just to avoid using globals and ease testing.
}{ // Values may define defaults for some fields and must setup some types.
	Port:      appcfg.MustPort("443"), // Set default.
	BindPorts: appcfg.MustListenPortSlice("80", "443"),
	Timeout:   appcfg.MustDuration("30s"), // Set default.
	Retries:   appcfg.NewIntBetween(1, 3), // Configure value constraints, no default.
}

// initExtCfg should be called before calling fs.Parse() - it'll gather
// configuration values from all sources except flags, and will setup
// flags on given fs.
func initExtCfg(fs *flag.FlagSet) error {
	const envPrefix = "EXAMPLE_"
	fromEnv := appcfg.NewFromEnv(envPrefix, appcfg.FromEnvTrimSpace())
	err := appcfg.ProvideStruct(&extCfg, fromEnv) // Set appCfg fields from environment.

	extCfg.fs = fs
	appcfg.AddFlag(fs, &extCfg.Host, "host", "host to connect")
	appcfg.AddFlag(fs, &extCfg.Port, "port", "port to connect")
	appcfg.AddFlag(fs, &extCfg.BindPorts, "bind.ports", "ports to bind")
	appcfg.AddFlag(fs, &extCfg.Timeout, "timeout", "connect timeout")

	return err
}

// config contains validated values in a way convenient for your app.
type config struct {
	Host      string        // Not "",  set by $EXAMPLE_HOST or -host, no default.
	Port      int           // 1…65535, set by $EXAMPLE_PORT or -port, default 443.
	BindPorts []int         // 1…65535, set by $EXAMPLE_BIND_PORTS or -bind.ports, default 80,443.
	Timeout   time.Duration // Any,     set by -timeout,               default 30s.
	Retries   int           // 1…3,     set by $EXAMPLE_RETRIES,       no default.
}

// getCfg checks is all required config values was provided and converts
// them into structure convenient for your app.
func getCfg() (cfg *config, err error) {
	cfg = &config{
		Host:      extCfg.Host.Value(&err), // Value set err if field was not set.
		Port:      extCfg.Port.Value(&err),
		BindPorts: extCfg.BindPorts.Value(&err),
		Timeout:   extCfg.Timeout.Value(&err),
		Retries:   extCfg.Retries.Value(&err),
	}
	if err != nil {
		return nil, appcfg.WrapErr(err, extCfg.fs, &extCfg)
	}
	return cfg, nil
}

func main() {
	err := initExtCfg(flag.CommandLine)
	if err != nil {
		log.Print(err)
		return
	}

	flag.Parse()

	cfg, err := getCfg()
	if err != nil {
		log.Print(err)
		return
	}
	log.Printf("cfg: %#v", cfg)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFlag added in v0.5.0

func AddFlag(fs *flag.FlagSet, value flag.Value, name string, usage string)

AddFlag defines a flag with the specified name and usage string. Calling it again with same fs, value and name will have no effect.

func AddPFlag added in v0.5.0

func AddPFlag(fs *pflag.FlagSet, value pflag.Value, name string, usage string)

AddPFlag defines a flag with the specified name and usage string. Calling it again with same fs, value and name will have no effect.

func ProvideStruct

func ProvideStruct(cfg interface{}, providers ...Provider) error

ProvideStruct updates cfg using values from given providers. Given cfg must be a ref to struct with all exported fields having Value type and struct tag with tags for given providers. Current values in cfg, if any, will be used as defaults.

Providers will be called for each exported field in cfg, in order, with next provider will be called only if previous providers won't provide a value for a current field.

It is recommended to add cfg fields to FlagSet after all other providers will be applied - this way usage message on -h flag will be able to show values set by other providers as flag defaults.

Returns error if any provider will try to set invalid value.

func WrapErr

func WrapErr(err error, fs *flag.FlagSet, cfgs ...interface{}) error

WrapErr adds more details about err.Value (if err is a RequiredError) by looking for related flag name and field name/tags in given fs and cfgs, otherwise returns err as is.

func WrapPErr

func WrapPErr(err error, fs *pflag.FlagSet, cfgs ...interface{}) error

WrapPErr adds more details about err.Value (if err is a RequiredError) by looking for related flag name and field name/tags in given fs and cfgs, otherwise returns err as is.

Types

type Bool added in v0.3.0

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

Bool can be set to 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.

func MustBool added in v0.3.0

func MustBool(s string) Bool

MustBool returns Bool initialized with given value or panics.

func (*Bool) Get added in v0.3.0

func (v *Bool) Get() interface{}

Get implements flag.Getter interface.

func (*Bool) IsBoolFlag added in v0.9.0

func (*Bool) IsBoolFlag() bool

IsBoolFlag implements extended flag.Value interface.

func (*Bool) Set added in v0.3.0

func (v *Bool) Set(s string) error

Set implements flag.Value interface.

func (*Bool) String added in v0.3.0

func (v *Bool) String() string

String implements flag.Value interface.

func (*Bool) Type added in v0.3.0

func (*Bool) Type() string

Type implements pflag.Value interface.

func (*Bool) Value added in v0.3.0

func (v *Bool) Value(err *error) (val bool)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type BoolSlice added in v0.9.0

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

BoolSlice can be set to comma-separated strings 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.

func MustBoolSlice added in v0.9.0

func MustBoolSlice(ss ...string) BoolSlice

MustBoolSlice returns BoolSlice initialized with given values or panics.

func (*BoolSlice) Get added in v0.9.0

func (v *BoolSlice) Get() interface{}

Get implements flag.Getter interface.

func (*BoolSlice) IsBoolFlag added in v0.9.0

func (*BoolSlice) IsBoolFlag() bool

IsBoolFlag implements extended flag.Value interface.

func (*BoolSlice) Set added in v0.9.0

func (v *BoolSlice) Set(s string) error

Set implements flag.Value interface.

func (*BoolSlice) String added in v0.9.0

func (v *BoolSlice) String() string

String implements flag.Value interface.

func (*BoolSlice) Type added in v0.9.0

func (*BoolSlice) Type() string

Type implements pflag.Value interface.

func (*BoolSlice) Value added in v0.9.0

func (v *BoolSlice) Value(err *error) (val []bool)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Duration

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

Duration can be set only to string valid for time.ParseDuration().

func MustDuration

func MustDuration(s string) Duration

MustDuration returns Duration initialized with given value or panics.

func (*Duration) Get

func (v *Duration) Get() interface{}

Get implements flag.Getter interface.

func (*Duration) Set

func (v *Duration) Set(s string) error

Set implements flag.Value interface.

func (*Duration) String

func (v *Duration) String() string

String implements flag.Value interface.

func (*Duration) Type

func (*Duration) Type() string

Type implements pflag.Value interface.

func (*Duration) Value

func (v *Duration) Value(err *error) (val time.Duration)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type DurationSlice added in v0.9.0

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

DurationSlice can be set to comma-separated strings valid for time.ParseDuration().

func MustDurationSlice added in v0.9.0

func MustDurationSlice(ss ...string) DurationSlice

MustDurationSlice returns DurationSlice initialized with given values or panics.

func (*DurationSlice) Get added in v0.9.0

func (v *DurationSlice) Get() interface{}

Get implements flag.Getter interface.

func (*DurationSlice) Set added in v0.9.0

func (v *DurationSlice) Set(s string) error

Set implements flag.Value interface.

func (*DurationSlice) String added in v0.9.0

func (v *DurationSlice) String() string

String implements flag.Value interface.

func (*DurationSlice) Type added in v0.9.0

func (*DurationSlice) Type() string

Type implements pflag.Value interface.

func (*DurationSlice) Value added in v0.9.0

func (v *DurationSlice) Value(err *error) (val []time.Duration)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Endpoint

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

Endpoint can be set only to valid url with hostname. Also it'll trim all / symbols at end, to make it easier to append paths to endpoint.

func MustEndpoint

func MustEndpoint(s string) Endpoint

MustEndpoint returns Endpoint initialized with given value or panics.

func (*Endpoint) Get

func (v *Endpoint) Get() interface{}

Get implements flag.Getter interface.

func (*Endpoint) Set

func (v *Endpoint) Set(s string) error

Set implements flag.Value interface.

func (*Endpoint) String

func (v *Endpoint) String() string

String implements flag.Value interface.

func (*Endpoint) Type

func (*Endpoint) Type() string

Type implements pflag.Value interface.

func (*Endpoint) Value

func (v *Endpoint) Value(err *error) (val string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type EndpointSlice added in v0.9.0

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

EndpointSlice can be set to valid urls with hostname. Also it'll trim all / symbols at end, to make it easier to append paths to endpoint.

func MustEndpointSlice added in v0.9.0

func MustEndpointSlice(ss ...string) EndpointSlice

MustEndpointSlice returns EndpointSlice initialized with given values or panics.

func (*EndpointSlice) Get added in v0.9.0

func (v *EndpointSlice) Get() interface{}

Get implements flag.Getter interface.

func (*EndpointSlice) Set added in v0.9.0

func (v *EndpointSlice) Set(s string) error

Set implements flag.Value interface.

func (*EndpointSlice) String added in v0.9.0

func (v *EndpointSlice) String() string

String implements flag.Value interface.

func (*EndpointSlice) Type added in v0.9.0

func (*EndpointSlice) Type() string

Type implements pflag.Value interface.

func (*EndpointSlice) Value added in v0.9.0

func (v *EndpointSlice) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Float64 added in v0.4.0

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

Float64 can be set to 64-bit floating-point number.

func MustFloat64 added in v0.4.0

func MustFloat64(s string) Float64

MustFloat64 returns Float64 initialized with given value or panics.

func (*Float64) Get added in v0.4.0

func (v *Float64) Get() interface{}

Get implements flag.Getter interface.

func (*Float64) Set added in v0.4.0

func (v *Float64) Set(s string) error

Set implements flag.Value interface.

func (*Float64) String added in v0.4.0

func (v *Float64) String() string

String implements flag.Value interface.

func (*Float64) Type added in v0.4.0

func (*Float64) Type() string

Type implements pflag.Value interface.

func (*Float64) Value added in v0.4.0

func (v *Float64) Value(err *error) (val float64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Float64Slice added in v0.9.0

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

Float64Slice can be set to comma-separated 64-bit floating-point numbers.

func MustFloat64Slice added in v0.9.0

func MustFloat64Slice(ss ...string) Float64Slice

MustFloat64Slice returns Float64Slice initialized with given values or panics.

func (*Float64Slice) Get added in v0.9.0

func (v *Float64Slice) Get() interface{}

Get implements flag.Getter interface.

func (*Float64Slice) Set added in v0.9.0

func (v *Float64Slice) Set(s string) error

Set implements flag.Value interface.

func (*Float64Slice) String added in v0.9.0

func (v *Float64Slice) String() string

String implements flag.Value interface.

func (*Float64Slice) Type added in v0.9.0

func (*Float64Slice) Type() string

Type implements pflag.Value interface.

func (*Float64Slice) Value added in v0.9.0

func (v *Float64Slice) Value(err *error) (val []float64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type FromEnv

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

FromEnv implements Provider using value from environment variable with name defined by tag "env" with optional prefix.

func NewFromEnv

func NewFromEnv(prefix string, opts ...FromEnvOption) *FromEnv

NewFromEnv creates new FromEnv with optional prefix.

func (*FromEnv) Provide

func (f *FromEnv) Provide(value Value, _ string, tags Tags) (bool, error)

Provide implements Provider.

type FromEnvOption added in v0.8.0

type FromEnvOption func(*FromEnv)

FromEnvOption is an option for NewFromEnv.

func FromEnvTrimSpace added in v0.8.0

func FromEnvTrimSpace() FromEnvOption

FromEnvTrimSpace removes from environment variables value all leading and trailing white space, as defined by Unicode.

type HostPort added in v0.7.0

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

HostPort can be set to CIDR address.

func MustHostPort added in v0.7.0

func MustHostPort(s string) HostPort

MustHostPort returns HostPort initialized with given value or panics.

func (*HostPort) Get added in v0.7.0

func (v *HostPort) Get() interface{}

Get implements flag.Getter interface.

func (*HostPort) Set added in v0.7.0

func (v *HostPort) Set(s string) error

Set implements flag.Value interface.

func (*HostPort) String added in v0.7.0

func (v *HostPort) String() string

String implements flag.Value interface.

func (*HostPort) Type added in v0.7.0

func (*HostPort) Type() string

Type implements pflag.Value interface.

func (*HostPort) Value added in v0.7.0

func (v *HostPort) Value(err *error) (host string, port int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type HostPortSlice added in v0.9.0

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

HostPortSlice can be set to comma-separated CIDR addresses.

func MustHostPortSlice added in v0.9.0

func MustHostPortSlice(ss ...string) HostPortSlice

MustHostPortSlice returns HostPortSlice initialized with given values or panics.

func (*HostPortSlice) Get added in v0.9.0

func (v *HostPortSlice) Get() interface{}

Get implements flag.Getter interface.

func (*HostPortSlice) Set added in v0.9.0

func (v *HostPortSlice) Set(s string) error

Set implements flag.Value interface.

func (*HostPortSlice) String added in v0.9.0

func (v *HostPortSlice) String() string

String implements flag.Value interface.

func (*HostPortSlice) Type added in v0.9.0

func (*HostPortSlice) Type() string

Type implements pflag.Value interface.

func (*HostPortSlice) Value added in v0.9.0

func (v *HostPortSlice) Value(err *error) []HostPortTuple

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type HostPortTuple added in v0.9.0

type HostPortTuple struct {
	Host string
	Port int
}

type IPNet added in v0.6.0

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

IPNet can be set to CIDR address.

func MustIPNet added in v0.6.0

func MustIPNet(s string) IPNet

MustIPNet returns IPNet initialized with given value or panics.

func (*IPNet) Get added in v0.6.0

func (v *IPNet) Get() interface{}

Get implements flag.Getter interface.

func (*IPNet) Set added in v0.6.0

func (v *IPNet) Set(s string) error

Set implements flag.Value interface.

func (*IPNet) String added in v0.6.0

func (v *IPNet) String() string

String implements flag.Value interface.

func (*IPNet) Type added in v0.6.0

func (*IPNet) Type() string

Type implements pflag.Value interface.

func (*IPNet) Value added in v0.6.0

func (v *IPNet) Value(err *error) (val *net.IPNet)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type IPNetSlice added in v0.9.0

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

IPNetSlice can be set to comma-separated CIDR address.

func MustIPNetSlice added in v0.9.0

func MustIPNetSlice(ss ...string) IPNetSlice

MustIPNetSlice returns IPNetSlice initialized with given values or panics.

func (*IPNetSlice) Get added in v0.9.0

func (v *IPNetSlice) Get() interface{}

Get implements flag.Getter interface.

func (*IPNetSlice) Set added in v0.9.0

func (v *IPNetSlice) Set(s string) error

Set implements flag.Value interface.

func (*IPNetSlice) String added in v0.9.0

func (v *IPNetSlice) String() string

String implements flag.Value interface.

func (*IPNetSlice) Type added in v0.9.0

func (*IPNetSlice) Type() string

Type implements pflag.Value interface.

func (*IPNetSlice) Value added in v0.9.0

func (v *IPNetSlice) Value(err *error) (val []*net.IPNet)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Int added in v0.4.0

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

Int can be set to integer value. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustInt added in v0.4.0

func MustInt(s string) Int

MustInt returns Int initialized with given value or panics.

func (*Int) Get added in v0.4.0

func (v *Int) Get() interface{}

Get implements flag.Getter interface.

func (*Int) Set added in v0.4.0

func (v *Int) Set(s string) error

Set implements flag.Value interface.

func (*Int) String added in v0.4.0

func (v *Int) String() string

String implements flag.Value interface.

func (*Int) Type added in v0.4.0

func (*Int) Type() string

Type implements pflag.Value interface.

func (*Int) Value added in v0.4.0

func (v *Int) Value(err *error) (val int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Int64 added in v0.4.0

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

Int64 can be set to 64-bit integer value. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustInt64 added in v0.4.0

func MustInt64(s string) Int64

MustInt64 returns Int64 initialized with given value or panics.

func (*Int64) Get added in v0.4.0

func (v *Int64) Get() interface{}

Get implements flag.Getter interface.

func (*Int64) Set added in v0.4.0

func (v *Int64) Set(s string) error

Set implements flag.Value interface.

func (*Int64) String added in v0.4.0

func (v *Int64) String() string

String implements flag.Value interface.

func (*Int64) Type added in v0.4.0

func (*Int64) Type() string

Type implements pflag.Value interface.

func (*Int64) Value added in v0.4.0

func (v *Int64) Value(err *error) (val int64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Int64Slice added in v0.9.0

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

Int64Slice can be set to comma-separated 64-bit integer values. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustInt64Slice added in v0.9.0

func MustInt64Slice(ss ...string) Int64Slice

MustInt64Slice returns Int64Slice initialized with given values or panics.

func (*Int64Slice) Get added in v0.9.0

func (v *Int64Slice) Get() interface{}

Get implements flag.Getter interface.

func (*Int64Slice) Set added in v0.9.0

func (v *Int64Slice) Set(s string) error

Set implements flag.Value interface.

func (*Int64Slice) String added in v0.9.0

func (v *Int64Slice) String() string

String implements flag.Value interface.

func (*Int64Slice) Type added in v0.9.0

func (*Int64Slice) Type() string

Type implements pflag.Value interface.

func (*Int64Slice) Value added in v0.9.0

func (v *Int64Slice) Value(err *error) (val []int64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type IntBetween added in v0.2.0

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

IntBetween can be set to integer value between given (using NewIntBetween or MustIntBetween) min/max values (inclusive).

func MustIntBetween added in v0.2.0

func MustIntBetween(s string, min, max int) IntBetween

MustIntBetween returns IntBetween initialized with given value or panics.

func NewIntBetween added in v0.2.0

func NewIntBetween(min, max int) IntBetween

NewIntBetween returns IntBetween without value set.

func (*IntBetween) Get added in v0.2.0

func (v *IntBetween) Get() interface{}

Get implements flag.Getter interface.

func (*IntBetween) Set added in v0.2.0

func (v *IntBetween) Set(s string) error

Set implements flag.Value interface.

func (*IntBetween) String added in v0.2.0

func (v *IntBetween) String() string

String implements flag.Value interface.

func (*IntBetween) Type added in v0.2.0

func (*IntBetween) Type() string

Type implements pflag.Value interface.

func (*IntBetween) Value added in v0.2.0

func (v *IntBetween) Value(err *error) (val int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type IntBetweenSlice added in v0.9.0

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

IntBetweenSlice can be set to comma-separated integer values between given (using NewIntBetween or MustIntBetween) min/max values (inclusive).

func MustIntBetweenSlice added in v0.9.0

func MustIntBetweenSlice(min, max int, ss ...string) IntBetweenSlice

MustIntBetweenSlice returns IntBetweenSlice initialized with given value or panics.

func NewIntBetweenSlice added in v0.9.0

func NewIntBetweenSlice(min, max int) IntBetweenSlice

NewIntBetweenSlice returns IntBetweenSlice without value set.

func (*IntBetweenSlice) Get added in v0.9.0

func (v *IntBetweenSlice) Get() interface{}

Get implements flag.Getter interface.

func (*IntBetweenSlice) Set added in v0.9.0

func (v *IntBetweenSlice) Set(s string) error

Set implements flag.Value interface.

func (*IntBetweenSlice) String added in v0.9.0

func (v *IntBetweenSlice) String() string

String implements flag.Value interface.

func (*IntBetweenSlice) Type added in v0.9.0

func (*IntBetweenSlice) Type() string

Type implements pflag.Value interface.

func (*IntBetweenSlice) Value added in v0.9.0

func (v *IntBetweenSlice) Value(err *error) (val []int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type IntSlice added in v0.9.0

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

IntSlice can be set to comma-separated integer values. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustIntSlice added in v0.9.0

func MustIntSlice(ss ...string) IntSlice

MustIntSlice returns IntSlice initialized with given values or panics.

func (*IntSlice) Get added in v0.9.0

func (v *IntSlice) Get() interface{}

Get implements flag.Getter interface.

func (*IntSlice) Set added in v0.9.0

func (v *IntSlice) Set(s string) error

Set implements flag.Value interface.

func (*IntSlice) String added in v0.9.0

func (v *IntSlice) String() string

String implements flag.Value interface.

func (*IntSlice) Type added in v0.9.0

func (*IntSlice) Type() string

Type implements pflag.Value interface.

func (*IntSlice) Value added in v0.9.0

func (v *IntSlice) Value(err *error) (val []int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type ListenPort

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

ListenPort can be set to integer value between 0 and 65535.

func MustListenPort

func MustListenPort(s string) ListenPort

MustListenPort returns ListenPort initialized with given value or panics.

func (*ListenPort) Get

func (v *ListenPort) Get() interface{}

Get implements flag.Getter interface.

func (*ListenPort) Set

func (v *ListenPort) Set(s string) error

Set implements flag.Value interface.

func (*ListenPort) String

func (v *ListenPort) String() string

String implements flag.Value interface.

func (*ListenPort) Type

func (*ListenPort) Type() string

Type implements pflag.Value interface.

func (*ListenPort) Value

func (v *ListenPort) Value(err *error) (val int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type ListenPortSlice added in v0.9.0

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

ListenPortSlice can be set to comma-separated integer values between 0 and 65535.

func MustListenPortSlice added in v0.9.0

func MustListenPortSlice(ss ...string) ListenPortSlice

MustListenPortSlice returns ListenPortSlice initialized with given values or panics.

func (*ListenPortSlice) Get added in v0.9.0

func (v *ListenPortSlice) Get() interface{}

Get implements flag.Getter interface.

func (*ListenPortSlice) Set added in v0.9.0

func (v *ListenPortSlice) Set(s string) error

Set implements flag.Value interface.

func (*ListenPortSlice) String added in v0.9.0

func (v *ListenPortSlice) String() string

String implements flag.Value interface.

func (*ListenPortSlice) Type added in v0.9.0

func (*ListenPortSlice) Type() string

Type implements pflag.Value interface.

func (*ListenPortSlice) Value added in v0.9.0

func (v *ListenPortSlice) Value(err *error) (val []int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type NotEmptyString

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

NotEmptyString can be set to any string which contains at least one non-whitespace symbol.

func MustNotEmptyString

func MustNotEmptyString(s string) NotEmptyString

MustNotEmptyString returns NotEmptyString initialized with given value or panics.

func (*NotEmptyString) Get

func (v *NotEmptyString) Get() interface{}

Get implements flag.Getter interface.

func (*NotEmptyString) Set

func (v *NotEmptyString) Set(s string) error

Set implements flag.Value interface.

func (*NotEmptyString) String

func (v *NotEmptyString) String() string

String implements flag.Value interface.

func (*NotEmptyString) Type

func (*NotEmptyString) Type() string

Type implements pflag.Value interface.

func (*NotEmptyString) Value

func (v *NotEmptyString) Value(err *error) (val string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type NotEmptyStringArray added in v0.9.0

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

NotEmptyStringArray can be set to any strings which contains at least one non-whitespace symbol.

func MustNotEmptyStringArray added in v0.9.0

func MustNotEmptyStringArray(ss ...string) NotEmptyStringArray

MustNotEmptyStringArray returns NotEmptyStringArray initialized with given values or panics.

func (*NotEmptyStringArray) Get added in v0.9.0

func (v *NotEmptyStringArray) Get() interface{}

Get implements flag.Getter interface.

func (*NotEmptyStringArray) Set added in v0.9.0

func (v *NotEmptyStringArray) Set(s string) error

Set implements flag.Value interface.

func (*NotEmptyStringArray) String added in v0.9.0

func (v *NotEmptyStringArray) String() string

String implements flag.Value interface.

func (*NotEmptyStringArray) Type added in v0.9.0

func (*NotEmptyStringArray) Type() string

Type implements pflag.Value interface.

func (*NotEmptyStringArray) Value added in v0.9.0

func (v *NotEmptyStringArray) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type NotEmptyStringSlice added in v0.9.0

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

NotEmptyStringSlice can be set to any comma-separated strings which contains at least one non-whitespace symbol.

func MustNotEmptyStringSlice added in v0.9.0

func MustNotEmptyStringSlice(ss ...string) NotEmptyStringSlice

MustNotEmptyStringSlice returns NotEmptyStringSlice initialized with given values or panics.

func (*NotEmptyStringSlice) Get added in v0.9.0

func (v *NotEmptyStringSlice) Get() interface{}

Get implements flag.Getter interface.

func (*NotEmptyStringSlice) Set added in v0.9.0

func (v *NotEmptyStringSlice) Set(s string) error

Set implements flag.Value interface.

func (*NotEmptyStringSlice) String added in v0.9.0

func (v *NotEmptyStringSlice) String() string

String implements flag.Value interface.

func (*NotEmptyStringSlice) Type added in v0.9.0

func (*NotEmptyStringSlice) Type() string

Type implements pflag.Value interface.

func (*NotEmptyStringSlice) Value added in v0.9.0

func (v *NotEmptyStringSlice) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type OneOfString

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

OneOfString can be set to any of predefined (using NewOneOfString or MustOneOfString) values.

func MustOneOfString

func MustOneOfString(s string, oneOf []string) OneOfString

MustOneOfString returns OneOfString initialized with given value or panics.

func NewOneOfString

func NewOneOfString(oneOf []string) OneOfString

NewOneOfString returns OneOfString without value set.

func (*OneOfString) Get

func (v *OneOfString) Get() interface{}

Get implements flag.Getter interface.

func (*OneOfString) Set

func (v *OneOfString) Set(s string) error

Set implements flag.Value interface.

func (*OneOfString) String

func (v *OneOfString) String() string

String implements flag.Value interface.

func (*OneOfString) Type

func (*OneOfString) Type() string

Type implements pflag.Value interface.

func (*OneOfString) Value

func (v *OneOfString) Value(err *error) (val string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type OneOfStringSlice added in v0.9.0

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

OneOfStringSlice can be set to predefined (using NewOneOfString or MustOneOfString) comma-separated strings.

func MustOneOfStringSlice added in v0.9.0

func MustOneOfStringSlice(oneOf []string, ss ...string) OneOfStringSlice

MustOneOfStringSlice returns OneOfStringSlice initialized with given value or panics.

func NewOneOfStringSlice added in v0.9.0

func NewOneOfStringSlice(oneOf []string) OneOfStringSlice

NewOneOfStringSlice returns OneOfStringSlice without value set.

func (*OneOfStringSlice) Get added in v0.9.0

func (v *OneOfStringSlice) Get() interface{}

Get implements flag.Getter interface.

func (*OneOfStringSlice) Set added in v0.9.0

func (v *OneOfStringSlice) Set(s string) error

Set implements flag.Value interface.

func (*OneOfStringSlice) String added in v0.9.0

func (v *OneOfStringSlice) String() string

String implements flag.Value interface.

func (*OneOfStringSlice) Type added in v0.9.0

func (*OneOfStringSlice) Type() string

Type implements pflag.Value interface.

func (*OneOfStringSlice) Value added in v0.9.0

func (v *OneOfStringSlice) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Port

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

Port can be set to integer value between 1 and 65535.

func MustPort

func MustPort(s string) Port

MustPort returns Port initialized with given value or panics.

func (*Port) Get

func (v *Port) Get() interface{}

Get implements flag.Getter interface.

func (*Port) Set

func (v *Port) Set(s string) error

Set implements flag.Value interface.

func (*Port) String

func (v *Port) String() string

String implements flag.Value interface.

func (*Port) Type

func (*Port) Type() string

Type implements pflag.Value interface.

func (*Port) Value

func (v *Port) Value(err *error) (val int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type PortSlice added in v0.9.0

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

PortSlice can be set to comma-separated integer values between 1 and 65535.

func MustPortSlice added in v0.9.0

func MustPortSlice(ss ...string) PortSlice

MustPortSlice returns PortSlice initialized with given values or panics.

func (*PortSlice) Get added in v0.9.0

func (v *PortSlice) Get() interface{}

Get implements flag.Getter interface.

func (*PortSlice) Set added in v0.9.0

func (v *PortSlice) Set(s string) error

Set implements flag.Value interface.

func (*PortSlice) String added in v0.9.0

func (v *PortSlice) String() string

String implements flag.Value interface.

func (*PortSlice) Type added in v0.9.0

func (*PortSlice) Type() string

Type implements pflag.Value interface.

func (*PortSlice) Value added in v0.9.0

func (v *PortSlice) Value(err *error) (val []int)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Provider

type Provider interface {
	Provide(value Value, name string, tags Tags) (provided bool, err error)
}

Provider tries to find and set value based on it name and/or tags.

type RequiredError

type RequiredError struct{ Value }

RequiredError is returned from Value(&err) methods if value wasn't set.

func (*RequiredError) Error

func (*RequiredError) Error() string

Error implements error interface.

type String

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

String can be set to any string, even empty.

func MustString

func MustString(s string) String

MustString returns String initialized with given value or panics.

func (*String) Get

func (v *String) Get() interface{}

Get implements flag.Getter interface.

func (*String) Set

func (v *String) Set(s string) error

Set implements flag.Value interface.

func (*String) String

func (v *String) String() string

String implements flag.Value interface.

func (*String) Type

func (*String) Type() string

Type implements pflag.Value interface.

func (*String) Value

func (v *String) Value(err *error) (val string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type StringArray added in v0.9.0

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

StringArray can be set to any strings, even empty.

func MustStringArray added in v0.9.0

func MustStringArray(ss ...string) StringArray

MustStringArray returns StringArray initialized with given values or panics.

func (*StringArray) Get added in v0.9.0

func (v *StringArray) Get() interface{}

Get implements flag.Getter interface.

func (*StringArray) Set added in v0.9.0

func (v *StringArray) Set(s string) error

Set implements flag.Value interface.

func (*StringArray) String added in v0.9.0

func (v *StringArray) String() string

String implements flag.Value interface.

func (*StringArray) Type added in v0.9.0

func (*StringArray) Type() string

Type implements pflag.Value interface.

func (*StringArray) Value added in v0.9.0

func (v *StringArray) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type StringSlice added in v0.9.0

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

StringSlice can be set to any comma-separated strings, even empty.

func MustStringSlice added in v0.9.0

func MustStringSlice(ss ...string) StringSlice

MustStringSlice returns StringSlice initialized with given values or panics.

func (*StringSlice) Get added in v0.9.0

func (v *StringSlice) Get() interface{}

Get implements flag.Getter interface.

func (*StringSlice) Set added in v0.9.0

func (v *StringSlice) Set(s string) error

Set implements flag.Value interface.

func (*StringSlice) String added in v0.9.0

func (v *StringSlice) String() string

String implements flag.Value interface.

func (*StringSlice) Type added in v0.9.0

func (*StringSlice) Type() string

Type implements pflag.Value interface.

func (*StringSlice) Value added in v0.9.0

func (v *StringSlice) Value(err *error) (val []string)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Tags

type Tags interface {
	// Get returns the value associated with key. If there is no such
	// key, Get returns the empty string.
	Get(key string) string
	// Lookup returns the value associated with key. If the key is
	// present the value (which may be empty) is returned. Otherwise
	// the returned value will be the empty string. The ok return
	// value reports whether the key is present.
	Lookup(key string) (value string, ok bool)
}

Tags provide access to tags attached to some Value.

type Uint added in v0.4.0

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

Uint can be set to unsigned integer value. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustUint added in v0.4.0

func MustUint(s string) Uint

MustUint returns Uint initialized with given value or panics.

func (*Uint) Get added in v0.4.0

func (v *Uint) Get() interface{}

Get implements flag.Getter interface.

func (*Uint) Set added in v0.4.0

func (v *Uint) Set(s string) error

Set implements flag.Value interface.

func (*Uint) String added in v0.4.0

func (v *Uint) String() string

String implements flag.Value interface.

func (*Uint) Type added in v0.4.0

func (*Uint) Type() string

Type implements pflag.Value interface.

func (*Uint) Value added in v0.4.0

func (v *Uint) Value(err *error) (val uint)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Uint64 added in v0.4.0

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

Uint64 can be set to unsigned 64-bit integer value. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustUint64 added in v0.4.0

func MustUint64(s string) Uint64

MustUint64 returns Uint64 initialized with given value or panics.

func (*Uint64) Get added in v0.4.0

func (v *Uint64) Get() interface{}

Get implements flag.Getter interface.

func (*Uint64) Set added in v0.4.0

func (v *Uint64) Set(s string) error

Set implements flag.Value interface.

func (*Uint64) String added in v0.4.0

func (v *Uint64) String() string

String implements flag.Value interface.

func (*Uint64) Type added in v0.4.0

func (*Uint64) Type() string

Type implements pflag.Value interface.

func (*Uint64) Value added in v0.4.0

func (v *Uint64) Value(err *error) (val uint64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Uint64Slice added in v0.9.0

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

Uint64Slice can be set to comma-separated unsigned 64-bit integer values. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustUint64Slice added in v0.9.0

func MustUint64Slice(ss ...string) Uint64Slice

MustUint64Slice returns Uint64Slice initialized with given values or panics.

func (*Uint64Slice) Get added in v0.9.0

func (v *Uint64Slice) Get() interface{}

Get implements flag.Getter interface.

func (*Uint64Slice) Set added in v0.9.0

func (v *Uint64Slice) Set(s string) error

Set implements flag.Value interface.

func (*Uint64Slice) String added in v0.9.0

func (v *Uint64Slice) String() string

String implements flag.Value interface.

func (*Uint64Slice) Type added in v0.9.0

func (*Uint64Slice) Type() string

Type implements pflag.Value interface.

func (*Uint64Slice) Value added in v0.9.0

func (v *Uint64Slice) Value(err *error) (val []uint64)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type UintSlice added in v0.9.0

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

UintSlice can be set to comma-separated unsigned integer values. It's allowed to use 0b, 0o and 0x prefixes, and also underscores.

func MustUintSlice added in v0.9.0

func MustUintSlice(ss ...string) UintSlice

MustUintSlice returns UintSlice initialized with given values or panics.

func (*UintSlice) Get added in v0.9.0

func (v *UintSlice) Get() interface{}

Get implements flag.Getter interface.

func (*UintSlice) Set added in v0.9.0

func (v *UintSlice) Set(s string) error

Set implements flag.Value interface.

func (*UintSlice) String added in v0.9.0

func (v *UintSlice) String() string

String implements flag.Value interface.

func (*UintSlice) Type added in v0.9.0

func (*UintSlice) Type() string

Type implements pflag.Value interface.

func (*UintSlice) Value added in v0.9.0

func (v *UintSlice) Value(err *error) (val []uint)

Value is like Get except it returns zero value and set *err to RequiredError if unset.

type Value

type Value interface {
	String() string   // May be called on zero-valued receiver.
	Set(string) error // Unset value on error.
	Get() interface{} // Return nil when not set (no default and no Set() or last Set() failed).
	Type() string     // Value type for help/usage.
}

Value provides a way to set value of any type from (one or several) string with validation. It's suitable for receiving configuration from hardcoded defaults, environment variables, command line flags, etc.

Implements common interfaces for flags (flag.Value, flag.Getter, github.com/spf13/pflag.Value and github.com/urfave/cli.Generic), but require more strict semantics to distinguish between zero and unset values (Set resets value to unset on error, Get return nil on unset).

Type containing multiple values should either replace or append value (or values if it can parse multiple values from a string) on Set depending on call sequence: replace if Set was called after Get, otherwise append.

Types containing boolean value should also provide IsBoolFlag() method returning true.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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