Help us shape the future of Go! Take the 2026 Go Developer Survey

setting

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSourceProvidedValue = errors.New("no source provided a value")

Functions

func ParseBool

func ParseBool(s string) (bool, error)

func ParseText

func ParseText[T ~string](s string) (T, error)

func ParseTextUnmarshaler

func ParseTextUnmarshaler[T any, P interface {
	*T
	encoding.TextUnmarshaler
}](s string) (T, error)

Types

type DefaultSource

type DefaultSource func() (string, error)

func StaticDefault

func StaticDefault(v string) DefaultSource

func (DefaultSource) Describe

func (d DefaultSource) Describe(key string) string

func (DefaultSource) Lookup

func (d DefaultSource) Lookup(_ context.Context, _ string) (string, error)

type EnvKey

type EnvKey string

func (EnvKey) Describe

func (k EnvKey) Describe(key string) string

func (EnvKey) Lookup

func (k EnvKey) Lookup(_ context.Context, key string) (string, error)

type FallbackSource

type FallbackSource struct {
	Source
}

FallbackSource ranks the wrapped source below the environment, see Sources.ResolveSetting.

type FrontendSource

type FrontendSource struct {
	Source
}

FrontendSource ranks the wrapped source above the environment, see Sources.ResolveSetting.

type LookupSource

type LookupSource struct {
	MatchingKey string
	Description string
	Func        func(ctx context.Context) (string, error)
}

func (LookupSource) Describe

func (s LookupSource) Describe(key string) string

func (LookupSource) Lookup

func (s LookupSource) Lookup(ctx context.Context, key string) (string, error)

type Setting

type Setting[T any] struct {
	Env EnvKey

	// Short is plain text for a cobra flag, Long is Markdown for the Terraform provider's schema.
	// Both state facts about the setting rather than about a front end, so neither says "flag",
	// "block" or "attribute".
	Short func(envKey string) string
	Long  func(envKey string) string

	Default DefaultSource
	// Parse always receives a non-empty string: Resolve trims what a source returned and skips
	// that source when nothing is left.
	Parse func(v string) (T, error)
}

Setting declares one input. Its environment variable key is also its identity, so every setting can be controlled through the environment and there is no second identifier to hold in step.

func (Setting[T]) EnvKey

func (s Setting[T]) EnvKey() string

func (Setting[T]) Help

func (s Setting[T]) Help() string

func (Setting[T]) HelpMarkdown

func (s Setting[T]) HelpMarkdown() string

type Source

type Source interface {
	// Lookup returns an empty string and no error when it carries no value. An error stops the
	// whole resolution, so return one only for a condition no other source can make up for.
	Lookup(ctx context.Context, key string) (string, error)
	// Describe names this source to a person, so that a failed resolution can list what to set.
	Describe(key string) string
}

type Sources

type Sources []Source

func (Sources) ResolveSetting

func (sources Sources) ResolveSetting[T any](ctx context.Context, setting Setting[T], extraSources ...Source) (value T, err error)

ResolveSetting takes the value from the first source that carries one, in this order: FrontendSource, any other source, the environment, FallbackSource, the setting's default. Two sources of the same kind keep the order they were given in. It returns ErrNoSourceProvidedValue when every source stayed empty.

func (Sources) ResolveSettingWithSource added in v0.3.0

func (sources Sources) ResolveSettingWithSource[T any](ctx context.Context, setting Setting[T], extraSources ...Source) (value T, source Source, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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