Skip to main content

Viper

This interface defines the contract for a configuration management system, providing methods to bind flags and environment variables, read and watch configuration files, and access all settings. It enables flexible configuration loading and dynamic updates for applications.

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​

NameTypeDescription
flags*pflag.FlagSetThe pflag.FlagSet containing the command-line flags to bind to Viper.

Returns​

TypeDescription
errorAn error if the binding fails.

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​

NameTypeDescription
input...stringOne or more environment variable names to bind to Viper.

Returns​

TypeDescription
errorAn error if the binding fails.

AutomaticEnv()​

@classmethod
def AutomaticEnv() - > null

Enables automatic binding of environment variables, where Viper will attempt to match environment variables to configuration keys. This simplifies the process of using environment variables for configuration by removing the need for explicit binding.

Returns​

TypeDescription
nullThis method does not return any value.

ReadInConfig()​

@classmethod
def ReadInConfig() - > error

Reads the configuration file into Viper, populating its internal configuration map. This method must be called to load configuration settings from the specified file.

Returns​

TypeDescription
errorAn error if the configuration file cannot be read or parsed.

OnConfigChange()​

@classmethod
def OnConfigChange(
run: func(in fsnotify.Event)
) - > null

Registers a callback function to be executed when the configuration file changes. This allows applications to react dynamically to configuration updates without restarting.

Parameters​

NameTypeDescription
runfunc(in fsnotify.Event)The function to execute when a configuration change event occurs, receiving an fsnotify.Event as an argument.

Returns​

TypeDescription
nullThis method does not return any value.

WatchConfig()​

@classmethod
def WatchConfig() - > null

Starts watching for changes to the configuration file, enabling the OnConfigChange callback to be triggered. This initiates a background process to monitor the configuration file for modifications.

Returns​

TypeDescription
nullThis method does not return any value.

AllSettings()​

@classmethod
def AllSettings() - > map[string]interface{}

Retrieves all configuration settings currently loaded in Viper as a map. This provides a complete view of the current configuration state.

Returns​

TypeDescription
map[string]interface{}A map containing all configuration settings, where keys are strings and values can be of any type.

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 configuration file is active.

Returns​

TypeDescription
stringThe absolute path to the configuration file being used.

MergeConfig()​

@classmethod
def MergeConfig(
in: io.Reader
) - > error

Merges configuration settings from an io.Reader into the existing Viper configuration. This allows for dynamic loading and merging of configuration from various sources, with new values overriding existing ones.

Parameters​

NameTypeDescription
inio.ReaderThe io.Reader from which to read the configuration to be merged.

Returns​

TypeDescription
errorAn error if the configuration from the reader cannot be merged.