config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: Apache-2.0, MIT Imports: 10 Imported by: 16

Documentation

Overview

Package config allows to build up trees of option sources using the OptionSet interface. The tree is built up by calling the AddSource methods. This function just composes the tree, it does not propagate option declarations. Each node may therefore contain explicitly declared options. The option declarations are propgated up the tree if the it is explicitly completed or added to a flagset (AddToFlagSet) or another OptionSet (AddOptionsToSet). Once propagated the nodes cannot be modified anymore. This propagation may use the local option declarations to decide how those options should be visible in the next upper OptionSet. This package offers support for three kinds of propagations:

  • Flat: the optiuons is just propagated as it is
  • Prefixed: the option is propagated with a prefix (it changes its name)
  • Shared: The option is propagated as it is but shared with other parallel OptionSources
  • PrefixedShared: The option is propagated with a prefix. Additionally a shared Option with the flat name is propagated.

Once the the options are finally set by parsing a command line using a pflag.FlagSet the top level OptionSet must be evaluated calling the Evaluate method. This method may do semnatic checks but the main task is to propagate the settings down the tree. For Flat and Prefixed propagations there is always a dedicated single optio target directly filled by the FlagSet. But for shared options there might be multiple targets. This propagation is the task of the Evalue methods.

Index

Constants

This section is empty.

Variables

View Source
var (
	StringOption      = optionTypeImpl(tStringOption)
	StringArrayOption = optionTypeImpl(tStringArrayOption)
	IntOption         = optionTypeImpl(tIntOption)
	UintOption        = optionTypeImpl(tUintOption)
	BoolOption        = optionTypeImpl(tBoolOption)
	DurationOption    = optionTypeImpl(tDurationOption)
)

Functions

func IdenityStringMapper

func IdenityStringMapper(s string) string

func MapToArguments

func MapToArguments(prefix string, flags *pflag.FlagSet, data map[string]interface{}) ([]string, error)

func MergeConfig

func MergeConfig(yaml []byte, flags *pflag.FlagSet, override bool) error

MergeConfig merges a flagset with yaml or json config data

func MergeConfigFile

func MergeConfigFile(fileName string, flags *pflag.FlagSet, override bool) error

MergeConfigFile reads a yaml or json config file and merges it into a given flagset

func MergeFlags

func MergeFlags(flags *pflag.FlagSet, args []string, override bool) error

MergeFlags adds arguments to a flag set. Arguments that are already defined in the flag set are ignored if override is set to false If the flag is a slice override appends and does NOT replace the value. The pflag API does not allow to replace a value.

func ParseConfig

func ParseConfig(bytes []byte, flags *pflag.FlagSet) ([]string, error)

ParseConfig parses yaml or json data to a list of equivalent command line arguments

func Print

func Print(log Writer, gap string, opts Options)

func PrintfWriter

func PrintfWriter(msgfmt string, args ...interface{})

func ReadConfigFile

func ReadConfigFile(name string, flags *pflag.FlagSet) ([]string, error)

ReadConfigFile reads a yaml or json file are parses its content to a list of equivalent command line arguments

Types

type ArbitraryOption

type ArbitraryOption struct {
	Name        string
	Description string
	Default     interface{}
	Target      interface{}
	Type        OptionType
	FlagSet     *pflag.FlagSet
}

func (*ArbitraryOption) AddToCommand

func (this *ArbitraryOption) AddToCommand(cmd *cobra.Command)

func (*ArbitraryOption) BoolValue

func (this *ArbitraryOption) BoolValue() bool

func (*ArbitraryOption) Changed

func (this *ArbitraryOption) Changed() bool

func (*ArbitraryOption) DurationValue

func (this *ArbitraryOption) DurationValue() time.Duration

func (*ArbitraryOption) Flag

func (this *ArbitraryOption) Flag() *pflag.Flag

func (*ArbitraryOption) IntValue

func (this *ArbitraryOption) IntValue() int

func (*ArbitraryOption) IsArray

func (this *ArbitraryOption) IsArray() bool

func (*ArbitraryOption) IsSlice

func (this *ArbitraryOption) IsSlice() bool

func (*ArbitraryOption) StringArray

func (this *ArbitraryOption) StringArray() []string

func (*ArbitraryOption) StringValue

func (this *ArbitraryOption) StringValue() string

func (*ArbitraryOption) UintValue

func (this *ArbitraryOption) UintValue() uint

func (*ArbitraryOption) Value

func (this *ArbitraryOption) Value() interface{}

type DefaultOptionSet

type DefaultOptionSet struct {
	OptionSetProxy
	// contains filtered or unexported fields
}

func NewDefaultOptionSet

func NewDefaultOptionSet(name, prefix string) *DefaultOptionSet

func (*DefaultOptionSet) AddOption

func (this *DefaultOptionSet) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}

func (*DefaultOptionSet) AddOptionsToSet

func (this *DefaultOptionSet) AddOptionsToSet(set OptionSet)

func (*DefaultOptionSet) AddRenamedOption

func (this *DefaultOptionSet) AddRenamedOption(opt *ArbitraryOption, name, short string, desc string) interface{}

func (*DefaultOptionSet) AddSource

func (this *DefaultOptionSet) AddSource(key string, src OptionSource)

func (*DefaultOptionSet) AddToCommand

func (this *DefaultOptionSet) AddToCommand(cmd *cobra.Command)

func (*DefaultOptionSet) AddToFlags

func (this *DefaultOptionSet) AddToFlags(flags *pflag.FlagSet)

func (*DefaultOptionSet) Complete

func (this *DefaultOptionSet) Complete()

func (*DefaultOptionSet) Evaluate

func (this *DefaultOptionSet) Evaluate() error

func (*DefaultOptionSet) GetOption

func (this *DefaultOptionSet) GetOption(name string) *ArbitraryOption

func (*DefaultOptionSet) GetSource

func (this *DefaultOptionSet) GetSource(key string) OptionSource

func (*DefaultOptionSet) Name

func (this *DefaultOptionSet) Name() string

func (*DefaultOptionSet) Prefix

func (this *DefaultOptionSet) Prefix() string

func (*DefaultOptionSet) SetValidator

func (this *DefaultOptionSet) SetValidator(v OptionValidator)

func (*DefaultOptionSet) VisitOptions

func (this *DefaultOptionSet) VisitOptions(f OptionVisitor)

func (*DefaultOptionSet) VisitSources

func (this *DefaultOptionSet) VisitSources(f OptionSourceVisitor)

type GenericOptionSource

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

func NewGenericOptionSource

func NewGenericOptionSource(name, prefix string, descmapper StringMapper) *GenericOptionSource

func (*GenericOptionSource) AddBoolOption

func (this *GenericOptionSource) AddBoolOption(kind OptionKind, target *bool, name, short string, def bool, desc string) *bool

func (*GenericOptionSource) AddDurationOption

func (this *GenericOptionSource) AddDurationOption(kind OptionKind, target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration

func (*GenericOptionSource) AddIntOption

func (this *GenericOptionSource) AddIntOption(kind OptionKind, target *int, name, short string, def int, desc string) *int

func (*GenericOptionSource) AddOption

func (this *GenericOptionSource) AddOption(kind OptionKind, otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}

func (*GenericOptionSource) AddOptionsToSet

func (this *GenericOptionSource) AddOptionsToSet(set OptionSet)

func (*GenericOptionSource) AddStringArrayOption

func (this *GenericOptionSource) AddStringArrayOption(kind OptionKind, target *[]string, name, short string, def []string, desc string) *[]string

func (*GenericOptionSource) AddStringOption

func (this *GenericOptionSource) AddStringOption(kind OptionKind, target *string, name, short string, def string, desc string) *string

func (*GenericOptionSource) AddUintOption

func (this *GenericOptionSource) AddUintOption(kind OptionKind, target *uint, name, short string, def uint, desc string) *uint

func (*GenericOptionSource) Complete

func (this *GenericOptionSource) Complete()

func (*GenericOptionSource) Evaluate

func (this *GenericOptionSource) Evaluate() error

func (*GenericOptionSource) Flat

func (this *GenericOptionSource) Flat() OptionSet

func (*GenericOptionSource) GetOption

func (this *GenericOptionSource) GetOption(name string) *ArbitraryOption

func (*GenericOptionSource) GetSource

func (this *GenericOptionSource) GetSource(name string) OptionSource

func (*GenericOptionSource) Name

func (this *GenericOptionSource) Name() string

func (*GenericOptionSource) Prefix

func (this *GenericOptionSource) Prefix() string

func (*GenericOptionSource) Prefixed

func (this *GenericOptionSource) Prefixed() OptionSet

func (*GenericOptionSource) PrefixedShared

func (this *GenericOptionSource) PrefixedShared() OptionSet

func (*GenericOptionSource) Shared

func (this *GenericOptionSource) Shared() OptionSet

func (*GenericOptionSource) VisitOptions

func (this *GenericOptionSource) VisitOptions(f OptionVisitor)

func (*GenericOptionSource) VisitSources

func (this *GenericOptionSource) VisitSources(f OptionSourceVisitor)

type OptionCompleter

type OptionCompleter interface {
	Complete()
}

OptionCompleter must be impmented by OptionSource to execute code to complete the option set. It should lock the object for further changes of described options. Basically it must be called by the AddOptionsToSet prior to handling the actual option declarations if nested targets are involved to call AddOptionsToSet on nested targets to complete the actal set.

type OptionEvaluator

type OptionEvaluator interface {
	Evaluate() error
}

OptionEvaluator must be implemented by OptionSource to execute code after option parsing is completed check and evaluate the actual settings

type OptionGroup

type OptionGroup interface {
	Options
	OptionSourceSource
}

OptionGroup is a group set of options and option sources offering access to options

type OptionKind

type OptionKind func(t *GenericOptionSource) OptionSet

type OptionSet

type OptionSet interface {
	Options
	OptionSource
	OptionCompleter
	OptionEvaluator
	OptionSourceGroup

	AddStringOption(target *string, name, short, def string, desc string) *string
	AddStringArrayOption(target *[]string, name, short string, def []string, desc string) *[]string
	AddIntOption(target *int, name, short string, def int, desc string) *int
	AddUintOption(target *uint, name, short string, def uint, desc string) *uint
	AddBoolOption(target *bool, name, short string, def bool, desc string) *bool
	AddDurationOption(target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration

	AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}
	AddRenamedOption(opt *ArbitraryOption, name, short string, desc string) interface{}

	// AddToFlags must complete the actual source if it contains nested
	// option sources (implements OptionSourceSource) to figure out all relevant
	// options to add. Afterwards the set should contain all options finally
	// added to the flag set (queryable by GetOption).
	AddToFlags(flags *pflag.FlagSet)
}

OptionSet is an OptionSource that can be used to add arbitrary options, either directly or by adding further nested OptionSource s.

func Flat

Flat options are just added as they are to an outer option sink

func Prefixed

func Prefixed(t *GenericOptionSource) OptionSet

Prefixed options are just added with a name prefix to an outer option sink

func PrefixedShared

func PrefixedShared(t *GenericOptionSource) OptionSet

PrefixedShared options are prefixed options that additionally declares a shared flat option on the sink. If the flat option is changed the value is propagated to option sources declaring this shared option. If the perfixed option is set, only this source get the value.

func Shared

func Shared(t *GenericOptionSource) OptionSet

Shared options are flat options that can be shared with other OptionSources (if they are also shared). The value of this option is distributed to all shared option targets.

type OptionSetProxy

type OptionSetProxy func(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}

func (OptionSetProxy) AddBoolOption

func (p OptionSetProxy) AddBoolOption(target *bool, name, short string, def bool, desc string) *bool

func (OptionSetProxy) AddDurationOption

func (p OptionSetProxy) AddDurationOption(target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration

func (OptionSetProxy) AddIntOption

func (p OptionSetProxy) AddIntOption(target *int, name, short string, def int, desc string) *int

func (OptionSetProxy) AddOption

func (p OptionSetProxy) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}

func (OptionSetProxy) AddStringArrayOption

func (p OptionSetProxy) AddStringArrayOption(target *[]string, name, short string, def []string, desc string) *[]string

func (OptionSetProxy) AddStringOption

func (p OptionSetProxy) AddStringOption(target *string, name, short string, def string, desc string) *string

func (OptionSetProxy) AddUintOption

func (p OptionSetProxy) AddUintOption(target *uint, name, short string, def uint, desc string) *uint

type OptionSource

type OptionSource interface {
	// AddOptionsToSet must complete the actual source if it contains nested
	// option sources (implements OptionSourceSource) to figure out all relevant
	// options to add. This can be done explicitly or implicitly by forwording
	// the call to nested sources.
	AddOptionsToSet(set OptionSet)
}

OptionSource is the interface used to add arbitrary options to an OptionSet.

type OptionSourceGroup

type OptionSourceGroup interface {
	OptionSourceSource
	AddSource(key string, src OptionSource)
}

OptionSourceGroup is a modifyable group of OptionSource

type OptionSourceSource

type OptionSourceSource interface {
	Name() string
	VisitSources(OptionSourceVisitor)
	GetSource(key string) OptionSource
}

OptionSourceSource is a group of OptionSource

type OptionSourceVisitor

type OptionSourceVisitor func(string, OptionSource) bool

OptionSourceVisitor is used to visit sources in an OptionGroup

type OptionType

type OptionType interface {
	AddToFlags(flags *pflag.FlagSet, target interface{}, name, short string, def interface{}, desc string) interface{}
}

type OptionValidator

type OptionValidator func(name string, set OptionSet)

type OptionVisitor

type OptionVisitor func(*ArbitraryOption) bool

type Options

type Options interface {
	// GetOption gives access to configured options. Nested OptionSources are only
	// visible after the OptionSet has been completed.
	GetOption(name string) *ArbitraryOption

	VisitOptions(OptionVisitor)
}

Options is an element offering named options

type SharedOptionSet

type SharedOptionSet struct {
	*DefaultOptionSet
	// contains filtered or unexported fields
}

func NewSharedOptionSet

func NewSharedOptionSet(name, prefix string, descMapper StringMapper) *SharedOptionSet

func (*SharedOptionSet) AddOptionsToSet

func (this *SharedOptionSet) AddOptionsToSet(set OptionSet)

func (*SharedOptionSet) Evaluate

func (this *SharedOptionSet) Evaluate() error

func (*SharedOptionSet) Unshare

func (this *SharedOptionSet) Unshare(name string)

type StringMapper

type StringMapper func(string) string

type Validatable

type Validatable interface {
	SetValidator(validator OptionValidator)
}

type Writer

type Writer func(msgfmt string, args ...interface{})

Jump to

Keyboard shortcuts

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