Viper
This interface defines the contract for a configuration management system, providing methods to bind command-line flags and environment variables, read configuration files, and monitor for changes. It allows for flexible configuration loading and access to all settings.
Methods
BindPFlags()
@classmethod
def BindPFlags(
flags: *pflag.FlagSet
) - > error
Binds a set of pflag.FlagSet flags to Viper, allowing configuration values to be sourced from command-line arguments. This enables Viper to automatically read and prioritize values provided via command-line flags.
Parameters
| Name | Type | Description |
|---|---|---|
| flags | *pflag.FlagSet | The pflag.FlagSet containing the command-line flags to bind to Viper. |
Returns
| Type | Description |
|---|---|
error | An error if the binding fails, otherwise nil. |
BindEnv()
@classmethod
def BindEnv(
input: ...string
) - > error
Binds one or more environment variables to Viper, allowing configuration values to be sourced from the system's environment. This is useful for providing configuration without modifying configuration files.
Parameters
| Name | Type | Description |
|---|---|---|
| input | ...string | One or more environment variable names to bind to Viper. |
Returns
| Type | Description |
|---|---|
error | An error if the binding fails, otherwise nil. |
AutomaticEnv()
@classmethod
def AutomaticEnv()
Enables automatic binding of environment variables to Viper. This method configures Viper to automatically search for environment variables that match its configuration keys, making it easier to override settings via the environment.
ReadInConfig()
@classmethod
def ReadInConfig() - > error
Reads the configuration file into Viper, populating its internal settings. This method must be called to load configuration from the specified file path.
Returns
| Type | Description |
|---|---|
error | An error if the configuration file cannot be read or parsed, otherwise nil. |
OnConfigChange()
@classmethod
def OnConfigChange(
run: func(in fsnotify.Event)
)
Registers a callback function to be executed when the configuration file changes. This allows applications to react dynamically to configuration updates without restarting.
Parameters
| Name | Type | Description |
|---|---|---|
| run | func(in fsnotify.Event) | The function to execute when a configuration change event occurs. It receives an fsnotify.Event detailing the change. |
WatchConfig()
@classmethod
def WatchConfig()
Starts watching the configuration file for changes. This method enables the OnConfigChange callbacks to be triggered when the configuration file is modified.
AllSettings()
@classmethod
def AllSettings() - > map[string]interface{}
Retrieves all current configuration settings as a map. This method provides a complete snapshot of the configuration currently loaded in Viper.
Returns
| Type | Description |
|---|---|
map[string]interface{} | A map where keys are configuration paths (strings) and values are the corresponding configuration settings (interface{}). |
ConfigFileUsed()
@classmethod
def ConfigFileUsed() - > string
Returns the path to the configuration file that Viper is currently using. This is useful for debugging or informational purposes to confirm which file was loaded.
Returns
| Type | Description |
|---|---|
string | The absolute path to the configuration file being used. |
MergeConfig()
@classmethod
def MergeConfig(
in: io.Reader
) - > error
Merges configuration from an io.Reader into the existing Viper configuration. This allows for dynamic loading and merging of configuration from various sources, such as network streams or embedded resources.
Parameters
| Name | Type | Description |
|---|---|---|
| in | io.Reader | The io.Reader providing the configuration data to be merged. |
Returns
| Type | Description |
|---|---|
error | An error if the configuration from the reader cannot be merged, otherwise nil. |