Documentation
¶
Overview ¶
Package jety provides configuration management supporting JSON, ENV, TOML, and YAML formats.
It offers viper-like AutomaticEnv functionality with minimal dependencies, allowing configuration to be loaded from files and environment variables with automatic merging.
Configuration sources are layered with the following precedence (highest to lowest):
- Values set via Set() or SetString()/SetBool()
- Environment variables (optionally filtered by prefix)
- Values from config file via ReadInConfig()
- Default values set via SetDefault()
Basic usage:
jety.SetConfigFile("/etc/myapp/config.yaml")
jety.SetConfigType("yaml")
jety.SetEnvPrefix("MYAPP_")
jety.SetDefault("port", 8080)
if err := jety.ReadInConfig(); err != nil {
log.Fatal(err)
}
port := jety.GetInt("port")
For multiple independent configurations, create separate ConfigManager instances:
cm := jety.NewConfigManager()
cm.SetConfigFile("/etc/myapp/config.toml")
cm.SetConfigType("toml")
cm.ReadInConfig()
Index ¶
- Constants
- Variables
- func AllKeys() []string
- func AllSettings() map[string]any
- func ConfigFileUsed() string
- func Get(key string) any
- func GetBool(key string) bool
- func GetDuration(key string) time.Duration
- func GetFloat64(key string) float64
- func GetInt(key string) int
- func GetInt64(key string) int64
- func GetIntSlice(key string) []int
- func GetString(key string) string
- func GetStringMap(key string) map[string]any
- func GetStringSlice(key string) []string
- func IsSet(key string) bool
- func ReadInConfig() error
- func Set(key string, value any)
- func SetBool(key string, value bool)
- func SetConfigDir(path string)
- func SetConfigFile(file string)
- func SetConfigName(name string)
- func SetConfigType(configType string) error
- func SetDefault(key string, value any)
- func SetEnvPrefix(prefix string)
- func SetString(key string, value string)
- func Unmarshal(target any) error
- func UnmarshalKey(key string, target any) error
- func WriteConfig() error
- type ConfigManager
- func (c *ConfigManager) AllKeys() []string
- func (c *ConfigManager) AllSettings() map[string]any
- func (c *ConfigManager) ConfigFileUsed() string
- func (c *ConfigManager) Get(key string) any
- func (c *ConfigManager) GetBool(key string) bool
- func (c *ConfigManager) GetDuration(key string) time.Duration
- func (c *ConfigManager) GetFloat64(key string) float64
- func (c *ConfigManager) GetInt(key string) int
- func (c *ConfigManager) GetInt64(key string) int64
- func (c *ConfigManager) GetIntSlice(key string) []int
- func (c *ConfigManager) GetString(key string) string
- func (c *ConfigManager) GetStringMap(key string) map[string]any
- func (c *ConfigManager) GetStringSlice(key string) []string
- func (c *ConfigManager) IsSet(key string) bool
- func (c *ConfigManager) ReadInConfig() error
- func (c *ConfigManager) Set(key string, value any)
- func (c *ConfigManager) SetBool(key string, value bool)
- func (c *ConfigManager) SetConfigDir(path string)
- func (c *ConfigManager) SetConfigFile(file string)
- func (c *ConfigManager) SetConfigName(name string)
- func (c *ConfigManager) SetConfigType(ct string) error
- func (c *ConfigManager) SetDefault(key string, value any)
- func (c *ConfigManager) SetEnvPrefix(prefix string)
- func (c *ConfigManager) SetString(key string, value string)
- func (c *ConfigManager) Sub(key string) *ConfigManager
- func (c *ConfigManager) Unmarshal(target any) error
- func (c *ConfigManager) UnmarshalKey(key string, target any) error
- func (c *ConfigManager) WithEnvPrefix(prefix string) *ConfigManager
- func (c *ConfigManager) WriteConfig() error
- type ConfigMap
Constants ¶
const ( ConfigTypeTOML configType = "toml" ConfigTypeYAML configType = "yaml" ConfigTypeJSON configType = "json" )
Variables ¶
var ( ErrConfigFileNotFound = errors.New("config file not found") ErrConfigFileEmpty = errors.New("config file is empty") )
Functions ¶
func AllSettings ¶ added in v0.3.0
func ConfigFileUsed ¶
func ConfigFileUsed() string
func GetDuration ¶
func GetFloat64 ¶ added in v0.4.0
func GetIntSlice ¶
func GetStringMap ¶
func GetStringSlice ¶
func ReadInConfig ¶
func ReadInConfig() error
func SetConfigDir ¶ added in v0.3.0
func SetConfigDir(path string)
func SetConfigFile ¶
func SetConfigFile(file string)
func SetConfigName ¶
func SetConfigName(name string)
func SetConfigType ¶
func SetDefault ¶
func SetEnvPrefix ¶
func SetEnvPrefix(prefix string)
func UnmarshalKey ¶ added in v0.4.1
func WriteConfig ¶
func WriteConfig() error
Types ¶
type ConfigManager ¶
type ConfigManager struct {
// contains filtered or unexported fields
}
ConfigManager manages layered configuration from defaults, files, environment variables, and programmatic overrides.
func NewConfigManager ¶
func NewConfigManager() *ConfigManager
NewConfigManager creates a new ConfigManager with all environment variables loaded. Use ConfigManager.WithEnvPrefix or ConfigManager.SetEnvPrefix to filter by prefix.
func Sub ¶ added in v0.4.0
func Sub(key string) *ConfigManager
func WithEnvPrefix ¶ added in v0.3.0
func WithEnvPrefix(prefix string) *ConfigManager
func (*ConfigManager) AllKeys ¶ added in v0.3.0
func (c *ConfigManager) AllKeys() []string
AllKeys returns all keys from all configuration sources, deduplicated.
func (*ConfigManager) AllSettings ¶ added in v0.3.0
func (c *ConfigManager) AllSettings() map[string]any
AllSettings returns all settings as a flat map of key to value.
func (*ConfigManager) ConfigFileUsed ¶
func (c *ConfigManager) ConfigFileUsed() string
func (*ConfigManager) Get ¶
func (c *ConfigManager) Get(key string) any
func (*ConfigManager) GetBool ¶
func (c *ConfigManager) GetBool(key string) bool
func (*ConfigManager) GetDuration ¶
func (c *ConfigManager) GetDuration(key string) time.Duration
func (*ConfigManager) GetFloat64 ¶ added in v0.4.0
func (c *ConfigManager) GetFloat64(key string) float64
func (*ConfigManager) GetInt ¶
func (c *ConfigManager) GetInt(key string) int
func (*ConfigManager) GetInt64 ¶ added in v0.4.0
func (c *ConfigManager) GetInt64(key string) int64
func (*ConfigManager) GetIntSlice ¶
func (c *ConfigManager) GetIntSlice(key string) []int
func (*ConfigManager) GetString ¶
func (c *ConfigManager) GetString(key string) string
func (*ConfigManager) GetStringMap ¶
func (c *ConfigManager) GetStringMap(key string) map[string]any
func (*ConfigManager) GetStringSlice ¶
func (c *ConfigManager) GetStringSlice(key string) []string
func (*ConfigManager) IsSet ¶ added in v0.3.0
func (c *ConfigManager) IsSet(key string) bool
IsSet checks whether a key has been set in any configuration source.
func (*ConfigManager) ReadInConfig ¶
func (c *ConfigManager) ReadInConfig() error
func (*ConfigManager) Set ¶
func (c *ConfigManager) Set(key string, value any)
func (*ConfigManager) SetBool ¶
func (c *ConfigManager) SetBool(key string, value bool)
func (*ConfigManager) SetConfigDir ¶
func (c *ConfigManager) SetConfigDir(path string)
func (*ConfigManager) SetConfigFile ¶
func (c *ConfigManager) SetConfigFile(file string)
func (*ConfigManager) SetConfigName ¶
func (c *ConfigManager) SetConfigName(name string)
func (*ConfigManager) SetConfigType ¶
func (c *ConfigManager) SetConfigType(ct string) error
func (*ConfigManager) SetDefault ¶
func (c *ConfigManager) SetDefault(key string, value any)
func (*ConfigManager) SetEnvPrefix ¶
func (c *ConfigManager) SetEnvPrefix(prefix string)
SetEnvPrefix filters environment variables to only those starting with the given prefix, stripping the prefix from key names.
func (*ConfigManager) SetString ¶
func (c *ConfigManager) SetString(key string, value string)
func (*ConfigManager) Sub ¶ added in v0.4.0
func (c *ConfigManager) Sub(key string) *ConfigManager
Sub returns a new ConfigManager rooted at the given key. The key must refer to a map value (e.g., from a nested TOML/YAML/JSON section). The returned ConfigManager has the nested map loaded as its default config, and does not inherit the parent's environment prefix, overrides, or config file. Returns nil if the key does not exist or is not a map.
func (*ConfigManager) Unmarshal ¶ added in v0.4.1
func (c *ConfigManager) Unmarshal(target any) error
Unmarshal unmarshals the full combined configuration into the provided struct pointer. It works by marshaling the settings map to JSON and then unmarshaling into the target, so struct field tags should use `json:"key"`. The target must be a non-nil pointer to a struct.
func (*ConfigManager) UnmarshalKey ¶ added in v0.4.1
func (c *ConfigManager) UnmarshalKey(key string, target any) error
UnmarshalKey unmarshals a specific key's value into the provided struct pointer. The key must refer to a map value (e.g., a nested TOML/YAML/JSON section). Supports dot notation for nested keys.
func (*ConfigManager) WithEnvPrefix ¶
func (c *ConfigManager) WithEnvPrefix(prefix string) *ConfigManager
WithEnvPrefix filters environment variables to only those starting with the given prefix, stripping the prefix from key names. Returns the ConfigManager for chaining.
func (*ConfigManager) WriteConfig ¶
func (c *ConfigManager) WriteConfig() error