Documentation
¶
Index ¶
- func AllOptions() map[string]RegisteredOption
- type EnvVarsMap
- type Option
- func (o *Option[T]) AllowsEnv() bool
- func (o *Option[T]) AllowsFlag() bool
- func (o *Option[T]) AllowsViper() bool
- func (o *Option[T]) GetDefaultAsString() string
- func (o *Option[T]) GetEnv() string
- func (o *Option[T]) GetEnvKey() string
- func (o *Option[T]) GetFlagKey() string
- func (o *Option[T]) GetName() string
- func (o *Option[T]) GetViperKey() string
- func (o *Option[T]) SetAllowFromEnv(isAllowed bool)
- func (o *Option[T]) SetAllowFromFlag(isAllowed bool)
- func (o *Option[T]) SetAllowFromViper(isAllowed bool)
- func (o *Option[T]) SetEnvKey(in string)
- func (o *Option[T]) SetFlagKey(in string)
- func (o *Option[T]) SetViperKey(in string)
- func (o *Option[T]) Type() reflect.Type
- type OptionalValue
- type RegisteredOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllOptions ¶
func AllOptions() map[string]RegisteredOption
AllOptions returns all registered options
Types ¶
type EnvVarsMap ¶
EnvVarsMap maps environment variable keys to their values
func AllEnvValues ¶
func AllEnvValues() EnvVarsMap
AllEnvValues returns a map of all env keys to their current values
func ConfiguredEnvValues ¶
func ConfiguredEnvValues() EnvVarsMap
ConfiguredEnvValues returns only env values that are actually set (non-empty)
type Option ¶
type Option[T any] struct { // Name gives the option an identifier and is the only required field Name string // Keys for different sources - automatically derived from Name if not set EnvKey string FlagKey string ViperKey string // Default represents the default value when unset from all other sources Default T // Source permissions AllowFromEnv bool AllowFromFlag bool AllowFromViper bool }
Option represents a configuration value that can be sourced from environment variables, CLI flags, or viper (config file). The name should be in kebab-case (e.g., "data-dir").
func NewOption ¶
func NewOption[T any](name string, defaultValue T, opts ...OptionalValue) *Option[T]
NewOption creates and registers a new option. The name should be kebab-case (e.g., "data-dir"). By default, options are allowed from all sources (env, flag, viper).
func (*Option[T]) AllowsFlag ¶
func (*Option[T]) AllowsViper ¶
func (*Option[T]) GetDefaultAsString ¶
func (*Option[T]) GetFlagKey ¶
func (*Option[T]) GetViperKey ¶
func (*Option[T]) SetAllowFromEnv ¶
func (*Option[T]) SetAllowFromFlag ¶
func (*Option[T]) SetAllowFromViper ¶
func (*Option[T]) SetFlagKey ¶
func (*Option[T]) SetViperKey ¶
type OptionalValue ¶
type OptionalValue func(RegisteredOption)
OptionalValue is a functional option for configuring Option behavior
var WithoutEnv OptionalValue = func(o RegisteredOption) { o.SetEnvKey("") o.SetAllowFromEnv(false) }
WithoutEnv disables environment variable as a source
var WithoutFlag OptionalValue = func(o RegisteredOption) { o.SetFlagKey("") o.SetAllowFromFlag(false) }
WithoutFlag disables CLI flag as a source
var WithoutViper OptionalValue = func(o RegisteredOption) { o.SetViperKey("") o.SetAllowFromViper(false) }
WithoutViper disables viper/config file as a source
func WithEnvKey ¶
func WithEnvKey(key string) OptionalValue
WithEnvKey sets a custom environment variable key
func WithFlagKey ¶
func WithFlagKey(key string) OptionalValue
WithFlagKey sets a custom CLI flag key
func WithViperKey ¶
func WithViperKey(key string) OptionalValue
WithViperKey sets a custom viper key (for config file)
type RegisteredOption ¶
type RegisteredOption interface {
GetName() string
GetDefaultAsString() string
SetEnvKey(string)
GetEnvKey() string
GetEnv() string
SetAllowFromEnv(bool)
AllowsEnv() bool
SetFlagKey(string)
GetFlagKey() string
SetAllowFromFlag(bool)
AllowsFlag() bool
SetViperKey(string)
GetViperKey() string
SetAllowFromViper(bool)
AllowsViper() bool
Type() reflect.Type
}
RegisteredOption defines the interface for all configuration options. This allows uniform handling of options regardless of their underlying type.