Skip to content

Commit

Permalink
Config: Privatise Global config var
Browse files Browse the repository at this point in the history
We should *either* use a private var *or* use an accessor, but it
doesn't make sense to mix paradigms.
Since GetConfig() is well established this instead removes the limited uses of direct public access and adds a Setter
  • Loading branch information
gbjk committed Feb 2, 2024
1 parent 0d7a15c commit 6a782ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
9 changes: 7 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,9 +1850,14 @@ func (c *Config) UpdateConfig(configPath string, newCfg *Config, dryrun bool) er
return c.LoadConfig(configPath, dryrun)
}

// GetConfig returns a pointer to a configuration object
// GetConfig returns the global shared config instance
func GetConfig() *Config {
return &Cfg
return &cfg
}

// SetConfig sets the global shared config instance
func SetConfig(c *Config) {
cfg = *c
}

// RemoveExchange removes an exchange config
Expand Down
8 changes: 6 additions & 2 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ const (
DefaultUnsetAccountPlan = "accountPlan"
)

// Public errors exported by this package
var (
Cfg Config
m sync.Mutex
ErrExchangeNotFound = errors.New("exchange not found")
)

var (
cfg Config
m sync.Mutex
)

// Config is the overarching object that holds all the information for
// prestart management of Portfolio, Communications, Webserver and Enabled
// Exchanges
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func New() (*Engine, error) {
newEngineMutex.Lock()
defer newEngineMutex.Unlock()
var b Engine
b.Config = &config.Cfg
b.Config = config.GetConfig()

err := b.Config.LoadConfig("", false)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func main() {
if engine.Bot == nil || err != nil {
log.Fatalf("Unable to initialise bot engine. Error: %s\n", err)
}
config.Cfg = *engine.Bot.Config
config.SetConfig(engine.Bot.Config)

gctscript.Setup()

Expand Down

0 comments on commit 6a782ec

Please sign in to comment.