kelseyhightower / envconfig

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

Is there a supported way of specifying per-OS defaults? #162

Open m90 opened 4 years ago

m90 commented 4 years ago

I am building an application that supports Unix-like OSes and Windows. In my configuration definition I am storing certain file paths like:

type Config struct {
     SomeLocation string `default:"/etc/myapp"`
}

This default obviously won't work on Windows where I would need to have something like:

type Config struct {
     SomeLocation string `default:"%AppData%\myapp"`
}

The approach I am using right now is having a config_unix.go and config_windows.go which have the respective build tags. This solution is very brittle and repetitive as I need to make sure the two config type definitions are always in sync.

Is this scenario something that package envconfig supports or could support? Is there a better approach for solving my problem using conditional compilation?

abemedia commented 3 years ago

If I was you I'd just have one single config struct and set that default value elsewhere. For example you could prepopulate that value before calling Process and if it's set in the config it simply gets overwritten.