From 7d7b0b0fc9eb078001a010dcdab3aa97938806c3 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 19 Apr 2022 23:57:51 +0700 Subject: all: always call Environment init This is to make sure that all required fields that are empty in the configuration initialized to its default values. --- environment.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'environment.go') diff --git a/environment.go b/environment.go index 99dc773..fad9c4e 100644 --- a/environment.go +++ b/environment.go @@ -20,7 +20,8 @@ import ( ) const ( - defWuiAddress = "127.0.0.1:5380" + defListenAddress = "127.0.0.1:53" + defWuiAddress = "127.0.0.1:5380" ) const ( @@ -95,20 +96,19 @@ func LoadEnvironment(dirBase, fileConfig string) (env *Environment, err error) { env = newEnvironment(dirBase, fileConfig) - if len(fileConfig) == 0 { - _ = env.init() - return env, nil - } + if len(fileConfig) > 0 { + cfg, err = ini.Open(env.fileConfig) + if err != nil { + return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + } - cfg, err = ini.Open(env.fileConfig) - if err != nil { - return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + err = cfg.Unmarshal(env) + if err != nil { + return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + } } - err = cfg.Unmarshal(env) - if err != nil { - return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) - } + _ = env.init() return env, nil } @@ -130,7 +130,7 @@ func newEnvironment(dirBase, fileConfig string) *Environment { Address: defWuiAddress, }, ServerOptions: dns.ServerOptions{ - ListenAddress: "127.0.0.1:53", + ListenAddress: defListenAddress, }, } } @@ -141,7 +141,7 @@ func (env *Environment) init() (err error) { env.WUIListen = defWuiAddress } if len(env.ListenAddress) == 0 { - env.ListenAddress = "127.0.0.1:53" + env.ListenAddress = defListenAddress } if len(env.FileResolvConf) > 0 { _, _ = env.loadResolvConf() -- cgit v1.3