From 65ef23b79b9508652820267788bf9d755b7853bd Mon Sep 17 00:00:00 2001 From: Karan Sharma Date: Tue, 1 Oct 2019 16:19:40 +0530 Subject: [PATCH] feat: read config from env --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 2dde5c2..4f31570 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/knadh/koanf" "github.com/knadh/koanf/maps" "github.com/knadh/koanf/parsers/toml" + "github.com/knadh/koanf/providers/env" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/providers/posflag" "github.com/knadh/listmonk/manager" @@ -109,9 +110,16 @@ func init() { if os.IsNotExist(err) { logger.Fatal("config file not found. If there isn't one yet, run --new-config to generate one.") } - logger.Fatalf("error loadng config: %v.", err) + logger.Fatalf("error loadng config from file: %v.", err) } } + // Load environment variables and merge into the loaded config. + if err := ko.Load(env.Provider("LISTMONK_", ".", func(s string) string { + return strings.Replace(strings.ToLower( + strings.TrimPrefix(s, "LISTMONK_")), "__", ".", -1) + }), nil); err != nil { + logger.Fatalf("error loading config from env: %v", err) + } ko.Load(posflag.Provider(f, ".", ko), nil) }