refactor(util): use `LogPrivacy` global instead of `GetConfig`

A global is a global, but at least this one is more targeted.
This commit is contained in:
ThinkChaos 2023-11-21 21:00:59 -05:00
parent 9760735f3a
commit 02cf4903f3
2 changed files with 25 additions and 9 deletions

View File

@ -18,6 +18,7 @@ import (
. "github.com/0xERR0R/blocky/config/migration" //nolint:revive,stylecheck
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/util"
"github.com/creasty/defaults"
"gopkg.in/yaml.v2"
)
@ -391,7 +392,7 @@ var (
)
// LoadConfig creates new config from YAML file or a directory containing YAML files
func LoadConfig(path string, mandatory bool) (*Config, error) {
func LoadConfig(path string, mandatory bool) (rCfg *Config, rerr error) {
cfgLock.Lock()
defer cfgLock.Unlock()
@ -400,14 +401,21 @@ func LoadConfig(path string, mandatory bool) (*Config, error) {
return nil, err
}
defer func() {
if rerr == nil {
util.LogPrivacy.Store(rCfg.Log.Privacy)
// We're still holding the lock
config = rCfg
}
}()
fs, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) && !mandatory {
// config file does not exist
// return config with default values
config = &cfg
return config, nil
return &cfg, nil
}
return nil, fmt.Errorf("can't read config file(s): %w", err)
@ -433,8 +441,6 @@ func LoadConfig(path string, mandatory bool) (*Config, error) {
return nil, err
}
config = &cfg
return &cfg, nil
}

View File

@ -9,19 +9,29 @@ import (
"regexp"
"sort"
"strings"
"sync/atomic"
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/log"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
var alphanumeric = regexp.MustCompile("[a-zA-Z0-9]")
//nolint:gochecknoglobals
var (
// To avoid making this package depend on config, we use a global
// that is set at config load.
// Ideally we'd move the obfuscate code somewhere else (maybe into `log`),
// but that would require also moving all its dependencies.
// This is good enough for now.
LogPrivacy atomic.Bool
alphanumeric = regexp.MustCompile("[a-zA-Z0-9]")
)
// Obfuscate replaces all alphanumeric characters with * to obfuscate user sensitive data if LogPrivacy is enabled
func Obfuscate(in string) string {
if config.GetConfig().Log.Privacy {
if LogPrivacy.Load() {
return alphanumeric.ReplaceAllString(in, "*")
}