kelseyhightower / envconfig

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

Add support for base64-encoded []byte field. #42

Closed ibrt closed 8 years ago

ibrt commented 8 years ago

The use case I have for this is encryption keys.

teepark commented 8 years ago

Thanks for this!

If I'm reading it correctly, this change assumes that any []byte field will be base64 (specifically with StdEncoding). That seems like a higher-level assumption than we should be making here.

Rather than building this into envconfig itself, it might be a great use-case for a custom decoder from #35 (I can merge that now -- it's sat for a while waiting feedback) roughly like this:

type b64 []byte
func (b *b64) Decode(value string) error {
    bytes, err := base64.StdEncoding.DecodeString(value)
    if err != nil {
        return err
    }
    *b = bytes
    return nil
}

var conf = struct {
    EncryptionKey b64
}{}

func init() {
    envconfig.MustProcess("prefix", &conf)
}