option

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

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

type EnvVarsMap map[string]string

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]) AllowsEnv

func (o *Option[T]) AllowsEnv() bool

func (*Option[T]) AllowsFlag

func (o *Option[T]) AllowsFlag() bool

func (*Option[T]) AllowsViper

func (o *Option[T]) AllowsViper() bool

func (*Option[T]) GetDefaultAsString

func (o *Option[T]) GetDefaultAsString() string

func (*Option[T]) GetEnv

func (o *Option[T]) GetEnv() string

func (*Option[T]) GetEnvKey

func (o *Option[T]) GetEnvKey() string

func (*Option[T]) GetFlagKey

func (o *Option[T]) GetFlagKey() string

func (*Option[T]) GetName

func (o *Option[T]) GetName() string

func (*Option[T]) GetViperKey

func (o *Option[T]) GetViperKey() string

func (*Option[T]) SetAllowFromEnv

func (o *Option[T]) SetAllowFromEnv(isAllowed bool)

func (*Option[T]) SetAllowFromFlag

func (o *Option[T]) SetAllowFromFlag(isAllowed bool)

func (*Option[T]) SetAllowFromViper

func (o *Option[T]) SetAllowFromViper(isAllowed bool)

func (*Option[T]) SetEnvKey

func (o *Option[T]) SetEnvKey(in string)

func (*Option[T]) SetFlagKey

func (o *Option[T]) SetFlagKey(in string)

func (*Option[T]) SetViperKey

func (o *Option[T]) SetViperKey(in string)

func (*Option[T]) Type

func (o *Option[T]) Type() reflect.Type

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.

Jump to

Keyboard shortcuts

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