Dropwizard-like configuration file parser
rcrowley opened this issue · comments
@mihasya's favorite thing about Dropwizard is the way configuration classes are declared and instantiated from YAML files. The flags
package notwithstanding, Tiger Tonic should really have the same.
The example configuration loader from the Boundary's Comparing Go and Java blog post is pretty simple and perhaps a good start:
type Config struct {
Db struct {
Name string
User string
Password string
}
}
func ReadConfig() Config {
if len(os.Args) != 2 {
log.Fatal("You must supply a configuration filename")
}
filename := os.Args[1]
bytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
var c Config
err = json.Unmarshal(bytes, &c)
if err != nil {
panic(err)
}
return c
}
It's impressive that you've read the entire Internet, @wadey.
And yes, that's an excellent start. I'd probably want to add support for non-zero default values by way of a tag.
I would prefer some sort of abstraction with a json impl that is easy enough to swap out for something like yml. Not sure you care :D I can try and come up with a way to do this if you'd like, not sure when I'll actually get to it
@mihasya How's this new change strike you?
AYE! 🤘 ✊ 🚬