providers

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// EnvProviderName represents the name of the environment variable-based configuration provider.
	EnvProviderName = "Environment Variables"
)

Variables

View Source
var (
	// ErrDotEnvFilePathNotSet indicates that the .env file path is not configured.
	ErrDotEnvFilePathNotSet = errors.New(".env file path is not set")
	// ErrDotEnvFileReadFailed indicates failure to read the .env file.
	ErrDotEnvFileReadFailed = errors.New("failed to read .env file")
	// ErrDotEnvParseFailed indicates failure to parse the .env file content.
	ErrDotEnvParseFailed = errors.New("failed to parse .env file")
	// ErrSetEnv indicates a failure call to os.Setenv().
	ErrSetEnv = errors.New("failed to set os env")
)
View Source
var (
	// ErrJSONFilePathNotSet indicates that the JSON file path is not configured.
	ErrJSONFilePathNotSet = errors.New("JSON file path is not set")
	// ErrJSONFileReadFailed indicates failure to read the JSON config file.
	ErrJSONFileReadFailed = errors.New("failed to read JSON config file")
	// ErrJSONDecodeFailed indicates failure to decode JSON content.
	ErrJSONDecodeFailed = errors.New("failed to decode JSON")
)

Functions

This section is empty.

Types

type DotEnvOption

type DotEnvOption func(*DotEnvProvider)

DotEnvOption is a function that configures a DotEnvProvider.

func WithDotEnvFileAppendToOSEnv

func WithDotEnvFileAppendToOSEnv(appendToOSEnv bool) DotEnvOption

WithDotEnvFileAppendToOSEnv sets the flag to append variables from the .env file to OS's env vars.

Default: false. Mirroring .env entries into the live process environment widens the blast radius of secrets (they are inherited by child processes and exposed via the OS) and lets a .env entry overwrite process variables, so it must be opted into explicitly.

func WithDotEnvFileFS

func WithDotEnvFileFS(fileFS fs.FS) DotEnvOption

WithDotEnvFileFS sets the fs of which to read the .env file from.

Default: sysfs.SysFS.

func WithDotEnvFileNotFoundPanic

func WithDotEnvFileNotFoundPanic(panicIfNotFound bool) DotEnvOption

WithDotEnvFileNotFoundPanic sets the flag to panic if the .env file is not found.

Default: true.

func WithDotEnvFilePath

func WithDotEnvFilePath(filePath string) DotEnvOption

WithDotEnvFilePath sets the .env file path.

func WithDotEnvSeparator

func WithDotEnvSeparator(sep string) DotEnvOption

WithDotEnvSeparator sets the separator for nested map values. Given a sep=__ variables like DATABASE__URL become database.url in the resulting map.

type DotEnvProvider

type DotEnvProvider struct {
	*providers.FSProvider
	*EnvProvider
	// contains filtered or unexported fields
}

DotEnvProvider reads configuration from .env file.

func NewDotEnvProvider

func NewDotEnvProvider(opts ...DotEnvOption) *DotEnvProvider

NewDotEnvProvider creates .env provider with options.

func (*DotEnvProvider) Load

func (p *DotEnvProvider) Load() (map[string]any, error)

Load implements the Provider interface.

func (*DotEnvProvider) Name

func (p *DotEnvProvider) Name() string

Name implements the Provider interface.

type EnvOption

type EnvOption func(*EnvProvider)

EnvOption is a function that configures an EnvProvider.

func WithEnvPrefix

func WithEnvPrefix(prefix string) EnvOption

WithEnvPrefix sets the environment variable prefix. Only variables starting with this prefix are included, and the prefix is removed from the key (e.g., "APP_" prefix, "APP_HOST" -> "HOST").

func WithEnvSeparator

func WithEnvSeparator(sep string) EnvOption

WithEnvSeparator sets the separator for nested map values. Given a sep=__ variables like DATABASE__URL become database.url in the resulting map.

type EnvProvider

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

EnvProvider reads configuration from environment variables.

func NewEnvProvider

func NewEnvProvider(opts ...EnvOption) *EnvProvider

NewEnvProvider creates an environment variable provider with options.

func (*EnvProvider) Load

func (p *EnvProvider) Load() (map[string]any, error)

Load implements the Provider interface.

func (*EnvProvider) Name

func (p *EnvProvider) Name() string

Name implements the Provider interface.

type JSONOption

type JSONOption func(*JSONProvider)

JSONOption is a function that configures a JSONProvider.

func WithJSONFileFS

func WithJSONFileFS(fileFS fs.FS) JSONOption

WithJSONFileFS sets the fs of which to read the JSON file from.

Default: sysfs.SysFS.

func WithJSONFilePath

func WithJSONFilePath(filePath string) JSONOption

WithJSONFilePath sets the JSON file path.

type JSONProvider

type JSONProvider struct {
	*providers.FSProvider
	// contains filtered or unexported fields
}

JSONProvider reads configuration from a JSON file.

func NewJSONProvider

func NewJSONProvider(opts ...JSONOption) *JSONProvider

NewJSONProvider creates a new file provider.

func (*JSONProvider) Load

func (p *JSONProvider) Load() (map[string]any, error)

Load implements the Provider interface.

func (*JSONProvider) Name

func (p *JSONProvider) Name() string

Name implements the Provider interface.

type Provider

type Provider interface {
	Name() string
	// Load reads configuration from the source and returns it as a map.
	// Keys should be hierarchical paths (e.g., "database.host").
	Load() (map[string]any, error)
}

Provider defines the interface for configuration providers. Implement this interface to create custom providers like env, json, yml, etc.

Jump to

Keyboard shortcuts

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