tkanos / gonfig

Manage Configuration file and environment in GO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Values not set when config file does not exist

jpmeijers opened this issue · comments

It can happen that the conf.json file does not exist, but that environment variables are set. In this case gonfig.GetConf returns an error and the variables in the configuration struct are not set.

The reason for no conf.json is that it is not committed to the git repo, but optionally added by Docker.
See:
https://docs.docker.com/engine/swarm/configs/

Workaround

Add an empty conf.json to the repo and hope that it will be overridden by Docker when a config is provided.

Example

AMQP_HOST=env_var.host

type Configuration struct {
	AmqpHost     string `env:"AMQP_HOST"`
	AmqpPort     string `env:"AMQP_PORT"`
	AmqpUser     string `env:"AMQP_USER"`
	AmqpPassword string `env:"AMQP_PASSWORD"`
}

var myConfiguration = Configuration{
	AmqpHost:     "default.host",
	AmqpPort:     "5672",
	AmqpUser:     "user",
	AmqpPassword: "password",
}

When an empty conf.json exists:

{
	"AmqpHost": "env_var.host",
	"AmqpPort": "5672",
	"AmqpUser": "user",
	"AmqpPassword": "password"
}

When no conf.json exists and the error returned is ignored:

{
	"AmqpHost": "default.host",
	"AmqpPort": "5672",
	"AmqpUser": "user",
	"AmqpPassword": "password"
}