Target functionality:
- Make it easy to load and save application configuration in a file (e.g.,
$HOME/.config/
) - Allow configuration value overrides via environment variables
- Enable a user to create or modify the configuration without a code editor
Since configurator
uses Generics, go
version 1.18
or greater is required.
$ go get github.com/noodnik2/configurator
LoadConfig[T any](configFile string, config *T) error
- loads configuration from a fileSaveConfig[T any](configFileName string, config T) error
- saves configuration to a fileEditConfig[T any](config *T) error
- invokes a user dialog to set or update the configuration
GetConfigEnvItems[T any](config T) ([]ConfigEnvItem, error)
- gets a list of configuration itemsSetConfigEnvItem[T any](config *T, envName, newValueAsString string) error
- updates a single configuration item
See the source code for details.
See example code that uses configurator
in the examples sub-folder.
configurator
builds on top of and extends three related foundational libraries:
- GoDotEnv - sets environment variables from a configuration file
- Envconfig - populates struct field values based on environment variables
- promptui - orchestrates a console-based dialog to set / modify configuration values
- Many cool features of Envconfig (such as the use of Prefixes, Complex Types and Nested Structs) aren't supported as of this writing, but could be added if / when needed.