kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.01k stars 377 forks source link

Feature Suggestion — Check arbitrary keys/values #126

Closed rossmcf closed 5 years ago

rossmcf commented 6 years ago

I work in a team that uses docker-compose. Commonly, we have docker-compose.yml files for each of our environments. It's a fairly common issue that we define new environment variables for a service, but forget to include them in the production environment's compose file, since it doesn't tend to be used locally.

I don't propose to make envconfig aware of docker compose, but I would like a way to 'check' an arbitrary set of keys/values against a spec. I can put together a tool to get the environment variables from the docker-compose file, but I can't currently pass those in to envconfig. Would you be open to adding a Check(prefix string, spec interface{}, kvs map[string]string) method? I'd be happy to submit a PR for it.

rossmcf commented 6 years ago

PR submitted. Suggestions welcome.

CAFxX commented 6 years ago

I don't understand the requirement. If you want to protect against forgetting to add the environment variables in production, can't you simply mark the fields as required?

rossmcf commented 5 years ago

@CAFxX We do that, but the issue we have is that we have to start up the service with each environment's config file in order to see the missing values. And in some cases we'd rather not start a service with production config, as it might start consuming from queues, for example.

CAFxX commented 5 years ago

Wouldn't having a dry-run/config validation flag that instructs the process to exit immediately after parsing its configuration be the simple and easy solution, then?

func main() {
  cfg := &Config{}
  if err := envconfig.Parse(cfg); err != nil {
    // Fatal(err)
  }

  // perform here any additional config validation, erroring out if it fails...

  if cfg.DryRun {
    os.Exit(0)
  }

  // actual program logic...
}

Alternatively, why can't you produce a parseable dump of the list of known envvars using Usagef/Usaget, and then use it during your build process to validate whatever preconditions have to be met for a successful build?

teepark commented 5 years ago

I had some feedback on the PR, but @CAFxX you're way ahead of me here. Great suggestions, thank you.