pathenvconfig
A lot like https://github.com/kelseyhightower/envconfig, but supports sourcing secrets from mounted files.
Instead of needing to specify a secret like POSTGRES_PASSWORD
as an environment variable (which might be a bad idea), you can specify POSTGRES_PASSWORD_FILE=/path/to/secret.txt
, where /path/to/secret.txt
contains the password.
Usage
Define a struct to hold your configuration values:
type MyConfig struct {
DatabaseConnectionString string `required:"true"`
TimeoutSeconds int `default:"10"`
}
Then you can load values into an instance of this struct my calling:
config := MyConfig{}
pathenvconfig.Process("MY_APP", &config)
This will populate the fields of the struct by inspecting environment variables. The first field can be specified with the environment variable
export MY_APP_DATABASE_CONNECTION_STRING="user=someone password=xyz dbname=mydb host=postgres port=5432"
Or you can provide an environment variable with the suffix _FILE
that contains a path to a file that contains the actual connection string:
export MY_APP_DATABASE_CONNECTION_STRING_FILE="/path/to/a/file.txt"
Nested Structs
Nested structs are also supported:
type DatabaseConfig struct {
User string `required:"true"`
Password string `required:"true"`
}
type MyConfig struct {
Database DatabaseConfig
TimeoutSeconds int `default:"10"`
}
The properties can be set with these environment variables:
MY_APP_DATABASE_USER
or MY_APP_DATABASE_USER_FILE
MY_APP_DATABASE_PASSWORD
or MY_APP_DATABASE_PASSWORD_FILE
MY_APP_TIMEOUT
or MY_APP_TIMEOUT_FILE