Skip to content

Commit

Permalink
refactor: remove the config global
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Nov 23, 2023
1 parent e4be0c0 commit 7d93ffb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
13 changes: 0 additions & 13 deletions config/config.go
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/miekg/dns"
Expand Down Expand Up @@ -385,17 +384,8 @@ func WithDefaults[T any]() (T, error) {
return cfg, nil
}

//nolint:gochecknoglobals
var (
config = &Config{}
cfgLock sync.RWMutex
)

// LoadConfig creates new config from YAML file or a directory containing YAML files
func LoadConfig(path string, mandatory bool) (rCfg *Config, rerr error) {
cfgLock.Lock()
defer cfgLock.Unlock()

cfg, err := WithDefaults[Config]()
if err != nil {
return nil, err
Expand All @@ -404,9 +394,6 @@ func LoadConfig(path string, mandatory bool) (rCfg *Config, rerr error) {
defer func() {
if rerr == nil {
util.LogPrivacy.Store(rCfg.Log.Privacy)

// We're still holding the lock
config = rCfg
}
}()

Expand Down
22 changes: 12 additions & 10 deletions config/config_test.go
Expand Up @@ -19,6 +19,8 @@ import (

var _ = Describe("Config", func() {
var (
c *Config

tmpDir *helpertest.TmpFolder
err error
)
Expand All @@ -32,9 +34,9 @@ var _ = Describe("Config", func() {
})

Describe("Deprecated parameters are converted", func() {
var c Config
BeforeEach(func() {
err := defaults.Set(&c)
c = new(Config)
err := defaults.Set(c)
Expect(err).Should(Succeed())
})

Expand Down Expand Up @@ -149,10 +151,10 @@ var _ = Describe("Config", func() {
confFile := writeConfigYml(tmpDir)
Expect(confFile.Error).Should(Succeed())

_, err = LoadConfig(confFile.Path, true)
c, err = LoadConfig(confFile.Path, true)
Expect(err).Should(Succeed())

defaultTestFileConfig()
defaultTestFileConfig(c)
})
})
When("Test file does not exist", func() {
Expand All @@ -169,7 +171,7 @@ var _ = Describe("Config", func() {
_, err := LoadConfig(tmpDir.Path, true)
Expect(err).Should(Succeed())

defaultTestFileConfig()
defaultTestFileConfig(c)
})

It("should ignore non YAML files", func() {
Expand Down Expand Up @@ -204,7 +206,7 @@ var _ = Describe("Config", func() {
cfgFile := tmpDir.CreateStringFile("config.yml", "malformed_config")
Expect(cfgFile.Error).Should(Succeed())

_, err = LoadConfig(cfgFile.Path, true)
c, err = LoadConfig(cfgFile.Path, true)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("wrong file structure"))
})
Expand Down Expand Up @@ -323,16 +325,16 @@ bootstrapDns:

When("config directory does not exist", func() {
It("should return error", func() {
_, err = LoadConfig(tmpDir.JoinPath("config.yml"), true)
c, err = LoadConfig(tmpDir.JoinPath("config.yml"), true)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("no such file or directory"))
})

It("should use default config if config is not mandatory", func() {
_, err = LoadConfig(tmpDir.JoinPath("config.yml"), false)
c, err = LoadConfig(tmpDir.JoinPath("config.yml"), false)

Expect(err).Should(Succeed())
Expect(config.Log.Level).Should(Equal(log.LevelInfo))
Expect(c.Log.Level).Should(Equal(log.LevelInfo))
})
})
})
Expand Down Expand Up @@ -768,7 +770,7 @@ bootstrapDns:
})
})

func defaultTestFileConfig() {
func defaultTestFileConfig(config *Config) {
Expect(config.Ports.DNS).Should(Equal(ListenConfig{"55553", ":55554", "[::1]:55555"}))
Expect(config.Upstreams.StartVerify).Should(BeFalse())
Expect(config.Upstreams.UserAgent).Should(Equal("testBlocky"))
Expand Down

0 comments on commit 7d93ffb

Please sign in to comment.