Documentation ¶
Overview ¶
github.com/usvc/go-config is a package to manage configuration values in libraries/applications.
Defining a configuration map:
var conf = config.Map{ "bool_key": &config.Bool{}, "float_key": &config.Float{}, "int_key": &config.Int{}, "int_slice_key": &config.IntSlice{}, "string_key": &config.String{}, "string_slice_key": &config.StringSlice{}, "uint_key": &config.Uint{}, }
Loading environment variables (assuming conf is defined):
conf.LoadFromEnvironment()
Applying to an instance of cobra.Command:
command := cobra.Command{ ... } conf.ApplyToCobra(&command)
Applying to an instance of cobra.Command persistently:
command := cobra.Command{ ... } conf.ApplyToCobraPersistent(&command)
Index ¶
- type Bool
- func (s *Bool) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *Bool) GetDefault() interface{}
- func (s *Bool) GetShorthand() string
- func (s *Bool) GetUsage() string
- func (s *Bool) GetValue() interface{}
- func (s *Bool) GetValuePointer() interface{}
- func (s Bool) IsSet() bool
- func (s Bool) IsSetExplicitlyByFlag() bool
- func (s *Bool) SetValue(value interface{}) (err error)
- type Config
- type Float
- func (s *Float) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *Float) GetDefault() interface{}
- func (s *Float) GetShorthand() string
- func (s *Float) GetUsage() string
- func (s *Float) GetValue() interface{}
- func (s *Float) GetValuePointer() interface{}
- func (s Float) IsSet() bool
- func (s Float) IsSetExplicitlyByFlag() bool
- func (s *Float) SetValue(value interface{}) (err error)
- type Int
- func (s *Int) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *Int) GetDefault() interface{}
- func (s *Int) GetShorthand() string
- func (s *Int) GetUsage() string
- func (s *Int) GetValue() interface{}
- func (s *Int) GetValuePointer() interface{}
- func (s Int) IsSet() bool
- func (s Int) IsSetExplicitlyByFlag() bool
- func (s *Int) SetValue(value interface{}) (err error)
- type IntSlice
- func (s *IntSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *IntSlice) GetDefault() interface{}
- func (s *IntSlice) GetShorthand() string
- func (s *IntSlice) GetUsage() string
- func (s *IntSlice) GetValue() interface{}
- func (s *IntSlice) GetValuePointer() interface{}
- func (s IntSlice) IsSet() bool
- func (s IntSlice) IsSetExplicitlyByFlag() bool
- func (s *IntSlice) SetValue(value interface{}) (err error)
- type Map
- func (m *Map) ApplyToCobra(command *cobra.Command)
- func (m *Map) ApplyToCobraPersistent(command *cobra.Command)
- func (m *Map) ApplyToFlagSet(flags *pflag.FlagSet)
- func (m Map) Get(id string) interface{}
- func (m Map) GetBool(id string) bool
- func (m Map) GetFloat(id string) float64
- func (m Map) GetInt(id string) int
- func (m Map) GetIntSlice(id string) []int
- func (m Map) GetString(id string) string
- func (m Map) GetStringSlice(id string) []string
- func (m Map) GetUint(id string) uint
- func (m *Map) LoadFromEnvironment()
- func (m Map) Reset() error
- type String
- func (s *String) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *String) GetDefault() interface{}
- func (s *String) GetShorthand() string
- func (s *String) GetUsage() string
- func (s *String) GetValue() interface{}
- func (s *String) GetValuePointer() interface{}
- func (s String) IsSet() bool
- func (s String) IsSetExplicitlyByFlag() bool
- func (s *String) SetValue(value interface{}) (err error)
- type StringSlice
- func (s *StringSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *StringSlice) GetDefault() interface{}
- func (s *StringSlice) GetShorthand() string
- func (s *StringSlice) GetUsage() string
- func (s *StringSlice) GetValue() interface{}
- func (s *StringSlice) GetValuePointer() interface{}
- func (s StringSlice) IsSet() bool
- func (s StringSlice) IsSetExplicitlyByFlag() bool
- func (s *StringSlice) SetValue(value interface{}) (err error)
- type Uint
- func (s *Uint) ApplyToFlagSet(name string, flags *pflag.FlagSet)
- func (s *Uint) GetDefault() interface{}
- func (s *Uint) GetShorthand() string
- func (s *Uint) GetUsage() string
- func (s *Uint) GetValue() interface{}
- func (s *Uint) GetValuePointer() interface{}
- func (s Uint) IsSet() bool
- func (s Uint) IsSetExplicitlyByFlag() bool
- func (s *Uint) SetValue(value interface{}) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct { Shorthand string Usage string Default bool Value bool // contains filtered or unexported fields }
Bool represents a configuration which should be interpreted as a boolean-typed value
func (*Bool) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*Bool) GetDefault ¶
func (s *Bool) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*Bool) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*Bool) GetValue ¶
func (s *Bool) GetValue() interface{}
GetValue returns the value of this configuration
func (*Bool) GetValuePointer ¶
func (s *Bool) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value. Be aware that this pointer points to the raw .Value value and does not take into account defaults that may have been specified which .GetValue() does
func (Bool) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (Bool) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
type Config ¶
type Config interface { ApplyToFlagSet(name string, flagset *pflag.FlagSet) GetDefault() interface{} GetShorthand() string GetUsage() string GetValue() interface{} GetValuePointer() interface{} IsSetExplicitlyByFlag() bool SetValue(value interface{}) (err error) }
Config defines an interface that all configuration keys should implement
type Float ¶
type Float struct { Shorthand string Usage string Default float64 Value float64 // contains filtered or unexported fields }
Float stores the configuration details for a floating point value
func (*Float) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*Float) GetDefault ¶
func (s *Float) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*Float) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*Float) GetValue ¶
func (s *Float) GetValue() interface{}
GetValue returns the value of this configuration
func (*Float) GetValuePointer ¶
func (s *Float) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value Be aware that this pointer points to the raw .Value value and does not take into account defaults that may have been specified which .GetValue() does
func (Float) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (Float) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
type Int ¶
type Int struct { Shorthand string Usage string Default int Value int // contains filtered or unexported fields }
Int represents a configuration which should be interpreted as a signed integer
func (*Int) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*Int) GetDefault ¶
func (s *Int) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*Int) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*Int) GetValue ¶
func (s *Int) GetValue() interface{}
GetValue returns the value of this configuration
func (*Int) GetValuePointer ¶
func (s *Int) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value
func (Int) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (Int) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
type IntSlice ¶
type IntSlice struct { Shorthand string Usage string Default []int Value []int // contains filtered or unexported fields }
IntSlice represents a configuration which should be interpreted as a slice of signed integers
func (*IntSlice) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*IntSlice) GetDefault ¶
func (s *IntSlice) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*IntSlice) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*IntSlice) GetValue ¶
func (s *IntSlice) GetValue() interface{}
GetValue returns the value of this configuration
func (*IntSlice) GetValuePointer ¶
func (s *IntSlice) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value
func (IntSlice) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (IntSlice) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
type Map ¶
Map stores a map of configurations identifed by a string key
func (*Map) ApplyToCobra ¶
ApplyToCobra applies the configuration stored in the instance of Map to a cobra.Command instance as flags (cannot be accessed by child commands)
func (*Map) ApplyToCobraPersistent ¶ added in v0.1.20
ApplyToCobraPersistent applies the configuration stored in the instance of Map to a cobra.Command instance as persistent flags (can be accessed by child commands)
func (*Map) ApplyToFlagSet ¶ added in v0.1.20
ApplyToFlagSet applies the configuration stored in the instance of Map to a provided set of flags
func (Map) Get ¶
Get retrieves the value of the configuration identified by the key :id as an `interface{}` type
func (Map) GetBool ¶
GetBool retrieves the value of the configuration identified by the key :id as a boolean type
func (Map) GetFloat ¶
GetFloat retrieves the value of the configuration identified by the key :id as a floating point type
func (Map) GetInt ¶
GetInt retrieves the value of the configuration identified by the key :id as an integer type
func (Map) GetIntSlice ¶
GetIntSlice retrieves the value of the configuration identified by the key :id as an integer slice type
func (Map) GetString ¶
GetString retrieves the value of the configuration identified by the key :id as a string type
func (Map) GetStringSlice ¶
GetStringSlice retrieves the value of the configuration identified by the key :id as a string slice type
func (Map) GetUint ¶
GetUint retrieves the value of the configuration identified by the key :id as an unsigned integer type
func (*Map) LoadFromEnvironment ¶
func (m *Map) LoadFromEnvironment()
LoadFromEnvironment loads the configuration from pre-defined environment variables
type String ¶
type String struct { Shorthand string Usage string Default string Value string // contains filtered or unexported fields }
String represents a configuration which should be interpreted as a string-typed value
func (*String) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*String) GetDefault ¶
func (s *String) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*String) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*String) GetValue ¶
func (s *String) GetValue() interface{}
GetValue returns the value of this configuration
func (*String) GetValuePointer ¶
func (s *String) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value
func (String) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (String) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
type StringSlice ¶
type StringSlice struct { Shorthand string Usage string Default []string Value []string // contains filtered or unexported fields }
StringSlice represents a configuration which should be represented as a slice of strings
func (*StringSlice) ApplyToFlagSet ¶
func (s *StringSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)
// ApplyToFlagSet applies the configuration to a provided flag set
func (*StringSlice) GetDefault ¶
func (s *StringSlice) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*StringSlice) GetShorthand ¶
func (s *StringSlice) GetShorthand() string
GetShorthand retrieves the short form of the flag if available
func (*StringSlice) GetUsage ¶
func (s *StringSlice) GetUsage() string
GetUsage retrieves the usage string for this configuration
func (*StringSlice) GetValue ¶
func (s *StringSlice) GetValue() interface{}
GetValue returns the value of this configuration
func (*StringSlice) GetValuePointer ¶
func (s *StringSlice) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value
func (StringSlice) IsSet ¶ added in v0.4.0
func (s StringSlice) IsSet() bool
IsSet returns ture if the value was set by the .SetValue method of this instance
func (StringSlice) IsSetExplicitlyByFlag ¶ added in v0.4.0
func (s StringSlice) IsSetExplicitlyByFlag() bool
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value
func (*StringSlice) SetValue ¶
func (s *StringSlice) SetValue(value interface{}) (err error)
SetValue sets the value of this configuration
type Uint ¶
type Uint struct { Shorthand string Usage string Default uint Value uint // contains filtered or unexported fields }
Uint represents a configuration which should be interpreted as an unsigned integer
func (*Uint) ApplyToFlagSet ¶
ApplyToFlagSet applies the configuration to a provided flag set
func (*Uint) GetDefault ¶
func (s *Uint) GetDefault() interface{}
GetDefault retrieves the default value of this configuration
func (*Uint) GetShorthand ¶
GetShorthand retrieves the short form of the flag if available
func (*Uint) GetValue ¶
func (s *Uint) GetValue() interface{}
GetValue returns the value of this configuration
func (*Uint) GetValuePointer ¶
func (s *Uint) GetValuePointer() interface{}
GetValuePointer returns a pointer that points to the instance of the configured value
func (Uint) IsSet ¶ added in v0.4.0
IsSet returns ture if the value was set by the .SetValue method of this instance
func (Uint) IsSetExplicitlyByFlag ¶ added in v0.4.0
IsSetExplicitlyByFlag returns true if the value was set by the user even if it equals the default value