Documentation
¶
Index ¶
- Constants
- func Close()
- func GetEnv(key string) string
- func Init(provider string, options *Options) error
- func SetBuildEnvironment(mode BuildEnv) error
- func SetEnv(key string, value string) error
- type BuildEnv
- type Config
- type ConfigFormat
- type ConfigSource
- type Koanf
- func (k *Koanf) Get(key string) interface{}
- func (k *Koanf) GetBool(key string) bool
- func (k *Koanf) GetBoolMap(key string) map[string]bool
- func (k *Koanf) GetBoolSlice(key string) []bool
- func (k *Koanf) GetDuration(key string) time.Duration
- func (k *Koanf) GetFloat64(key string) float64
- func (k *Koanf) GetFloat64Map(key string) map[string]float64
- func (k *Koanf) GetFloat64Slice(key string) []float64
- func (k *Koanf) GetInt64(key string) int64
- func (k *Koanf) GetInt64Map(key string) map[string]int64
- func (k *Koanf) GetInt64Slice(key string) []int64
- func (k *Koanf) GetString(key string) string
- func (k *Koanf) GetStringMap(key string) map[string]string
- func (k *Koanf) GetStringSlice(key string) []string
- func (k *Koanf) GetStringSliceMap(key string) map[string][]string
- func (k *Koanf) GetTime(key string, layout string) time.Time
- func (k *Koanf) RawStore() interface{}
- func (k *Koanf) Set(key string, value interface{}) error
- func (k *Koanf) Unmarshal(path string, cfg interface{}) error
- type Manager
- type Options
Constants ¶
const ( DefaultConfigFilePath string = "" DefaultConfigPathDelimiter string = "." ConfigProviderKoanf string = "koanf" )
const ( SourceFile ConfigSource = 1 // Config source file SourceRawBytes ConfigSource = 2 // Config source raw bytes FormatJson ConfigFormat = 1 // Format JSON )
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v1.0.1
func Close()
Close stops the active config manager and clears package-level state.
func GetEnv ¶
GetEnv retrieves the value of the environment variable identified by the given key. If the specified environment variable is not set, an empty string is returned.
Parameters:
- key: A string representing the environment variable name.
Returns:
- string: The value of the environment variable, or an empty string if not set.
func Init ¶
Init initializes the configuration system with a specified provider and configuration options. This function sets up the configuration provider if it's listed as allowed and validates the provided options.
Parameters:
- provider: A string representing the configuration provider to be used, which must be one of the allowed providers.
- options: A pointer to Options which contains necessary options that need to be validated and used during initialization.
Usage:
- This function should be called at the start of an application to set up the configuration provider.
- If the options are not valid or the provider is not allowed, the application will terminate with a fatal log error.
func SetBuildEnvironment ¶
SetBuildEnvironment sets the current build environment mode and updates the corresponding environment variable. It takes a BuildEnv value and sets it as the current build environment.
Parameters:
- mode: The BuildEnv value representing the desired build environment mode.
Usage:
- SetBuildEnvironment(EnvProduction) sets the environment to production mode.
- SetBuildEnvironment(EnvDevelopment) sets the environment to development mode.
func SetEnv ¶
SetEnv sets the value of the environment variable identified by the given key. If the environment variable does not exist, it is created.
Parameters:
- key: A string representing the environment variable name.
- value: A string representing the value to be set.
Returns:
- error: An error if the environment variable could not be set.
Types ¶
type BuildEnv ¶
type BuildEnv string
BuildEnv defines a type for representing build environment modes.
func GetBuildEnvironment ¶
func GetBuildEnvironment() BuildEnv
GetBuildEnvironment returns the current build environment mode.
Returns:
- BuildEnv: The current build environment mode.
Usage:
- env := GetBuildEnvironment() retrieves the current build environment mode.
type Config ¶
type Config interface {
RawStore() interface{}
Unmarshal(path string, cfg interface{}) error
Get(key string) interface{}
Set(key string, value interface{}) error
GetBool(key string) bool
GetInt64(key string) int64
GetFloat64(key string) float64
GetString(key string) string
GetTime(key string, layout string) time.Time
GetDuration(key string) time.Duration
GetBoolSlice(key string) []bool
GetInt64Slice(key string) []int64
GetFloat64Slice(key string) []float64
GetStringSlice(key string) []string
GetBoolMap(key string) map[string]bool
GetInt64Map(key string) map[string]int64
GetFloat64Map(key string) map[string]float64
GetStringMap(key string) map[string]string
GetStringSliceMap(key string) map[string][]string
// contains filtered or unexported methods
}
Config interface defines a set of methods for managing application configuration. It provides methods to retrieve and set configuration values in a type-safe way, as well as methods to handle complex data types and structures such as slices and maps.
Methods: - RawStore: Returns the raw, underlying storage mechanism used to store configuration data.
- Unmarshal: Populates a struct with configuration data based on a specified path within the configuration store.
- Get: Retrieves a value from the configuration as an interface{} based on the given key. - Set: Sets a value in the configuration store under the specified key.
- GetBool: Retrieves a boolean value from the configuration. - GetInt64: Retrieves an int64 value from the configuration. - GetFloat64: Retrieves a float64 value from the configuration. - GetString: Retrieves a string value from the configuration.
- GetTime: Retrieves a time value formatted according to the specified layout. - GetDuration: Retrieves a duration value from the configuration.
- GetBoolSlice: Retrieves a slice of boolean values from the configuration. - GetInt64Slice: Retrieves a slice of int64 values from the configuration. - GetFloat64Slice: Retrieves a slice of float64 values from the configuration. - GetStringSlice: Retrieves a slice of string values from the configuration.
- GetBoolMap: Retrieves a map of string keys to boolean values. - GetInt64Map: Retrieves a map of string keys to int64 values. - GetFloat64Map: Retrieves a map of string keys to float64 values. - GetStringMap: Retrieves a map of string keys to string values. - GetStringSliceMap: Retrieves a map of string keys to slices of string values.
Usage: This interface is used to abstract the details of configuration handling from the rest of the application. It allows for easy access to configuration values while maintaining flexibility in the storage mechanism.
Example:
func setupApp(cfg Config) {
port := cfg.GetInt64("server.port")
if cfg.GetBool("features.logging") {
setupLogging()
}
fmt.Println("Starting server on port:", port)
}
This interface can be implemented by different types of configuration providers (e.g., from files, environment variables, etc.), allowing for versatile and interchangeable configuration management strategies in applications.
func Load ¶
func Load() Config
Load returns a singleton instance of the Config interface, initializing it according to the configuration provider. This function ensures that the configuration instance is created only once (singleton pattern) using the specified provider options.
Returns:
- An instance of the Config interface, ready to be used throughout the application.
Usage:
- This function should be called to retrieve the configuration instance after it has been initialized with Init.
- It supports a thread-safe singleton pattern to ensure that only one configuration instance is active at any given time.
type ConfigFormat ¶
type ConfigFormat uint8 // Data format of the config (json/etc)
type ConfigSource ¶
type ConfigSource uint8 // Source of the config file (file/raw bytes)
type Koanf ¶
type Koanf struct {
// contains filtered or unexported fields
}
Koanf wraps the koanf.Koanf struct to provide additional methods for config management.
func (*Koanf) Get ¶
Get retrieves the value associated with the given key in the configuration data.
Parameters: - key: A string specifying the key in the configuration data.
Returns: - interface{}: The value associated with the key.
func (*Koanf) GetBool ¶
GetBool retrieves the boolean value associated with the given key in the configuration data.
func (*Koanf) GetBoolMap ¶
GetBoolMap retrieves the map of boolean values associated with the given key in the configuration data.
func (*Koanf) GetBoolSlice ¶
GetBoolSlice retrieves the slice of boolean values associated with the given key in the configuration data.
func (*Koanf) GetDuration ¶
GetDuration retrieves the time.Duration value associated with the given key in the configuration data.
func (*Koanf) GetFloat64 ¶
GetFloat64 retrieves the float64 value associated with the given key in the configuration data.
func (*Koanf) GetFloat64Map ¶
GetFloat64Map retrieves the map of float64 values associated with the given key in the configuration data.
func (*Koanf) GetFloat64Slice ¶
GetFloat64Slice retrieves the slice of float64 values associated with the given key in the configuration data.
func (*Koanf) GetInt64 ¶
GetInt64 retrieves the int64 value associated with the given key in the configuration data.
func (*Koanf) GetInt64Map ¶
GetInt64Map retrieves the map of int64 values associated with the given key in the configuration data.
func (*Koanf) GetInt64Slice ¶
GetInt64Slice retrieves the slice of int64 values associated with the given key in the configuration data.
func (*Koanf) GetString ¶
GetString retrieves the string value associated with the given key in the configuration data.
func (*Koanf) GetStringMap ¶
GetStringMap retrieves the map of string values associated with the given key in the configuration data.
func (*Koanf) GetStringSlice ¶
GetStringSlice retrieves the slice of string values associated with the given key in the configuration data.
func (*Koanf) GetStringSliceMap ¶
GetStringSliceMap retrieves the map of string slices associated with the given key in the configuration data.
func (*Koanf) GetTime ¶
GetTime retrieves the time.Time value associated with the given key in the configuration data.
Parameters: - key: A string specifying the key in the configuration data. - layout: The layout to parse the time value.
Returns: - time.Time: The parsed time value.
func (*Koanf) RawStore ¶
func (k *Koanf) RawStore() interface{}
RawStore returns the underlying koanf.Koanf instance.
func (*Koanf) Set ¶
Set sets the value for the given key in the configuration data.
Parameters: - key: A string specifying the key in the configuration data. - value: The value to be set.
Returns: - error: An error if setting the value fails, otherwise nil.
func (*Koanf) Unmarshal ¶
Unmarshal unmarshals the configuration data into the given struct based on the specified path.
Parameters: - path: A string specifying the path in the configuration data. - cfg: A pointer to the struct where the configuration data should be unmarshalled.
Returns: - error: An error if the unmarshalling fails, otherwise nil.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns a config instance and, when enabled, its watcher lifecycle.
type Options ¶
type Options struct {
Source ConfigSource
Format ConfigFormat
FilePath string
Content []byte
Delimiter string
EnableWatch bool
WatcherOptions *watcher.WatcherOptions
}
Options struct contains the necessary details to initialize and manage configurations. It defines parameters like source, format, file path, content, and delimiter for parsing configuration data.
Fields:
- Source: ConfigSource enum that specifies the source of configuration data (e.g., file, raw bytes).
- Format: ConfigFormat enum that defines the format of the configuration data (e.g., JSON, XML).
- FilePath: The file path to the configuration file if the source is a file. This must be readable.
- Content: A byte slice that holds raw configuration data if the source is set to raw bytes.
- Delimiter: A string that specifies the delimiter used in the configuration data for separating keys.
- EnableWatch: A boolean flag indicating whether to enable file watching for changes in the configuration.
- WatcherOptions: A pointer to watcher.WatcherOptions that contains options for the config watcher.
func (*Options) IsValid ¶
IsValid method validates the configuration options to ensure that all necessary parameters are correctly set and valid. It checks if the configuration source and format are supported and verifies file accessibility if the source is a file.
Returns:
- nil if all config options are valid.
- An error detailing what is incorrect or missing in the options.
Usage:
- Call IsValid before using the options to load the configuration to ensure all parameters meet the expected criteria.