Skip to content

Commit

Permalink
add parsing of Secret from ENV var
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmasek committed Jan 14, 2025
1 parent bd0b6f6 commit 0c3c780
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"time"

"github.com/caarlos0/env/v11"
Expand Down Expand Up @@ -215,6 +216,11 @@ func ConfigFromBytes(data []byte) (*Config, error) {
}
err = env.ParseWithOptions(config, env.Options{
Prefix: ENV_VAR_PREFIX,
FuncMap: map[reflect.Type]env.ParserFunc{
reflect.TypeOf(Secret{}): func(v string) (interface{}, error) {
return Secret{v}, nil
},
},
})
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ prefix: "[test]"
})
require.Equal(t, "h4xor", emailConfig.SmtpPassword.Get())
}

func TestSecretFromEnv(t *testing.T) {
err := os.Setenv("BEACON_EMAIL_SMTP_PASSWORD", "secr4t")
require.NoError(t, err)
conf, err := ConfigFromBytes([]byte(""))
require.NoError(t, err)
require.Equal(t, "secr4t", conf.EmailConf.SmtpPassword.Get())

}

0 comments on commit 0c3c780

Please sign in to comment.