From 523e109ef130d4c9aa4ca3a890817c9b8f15a45e Mon Sep 17 00:00:00 2001 From: Robert Ignat Date: Fri, 15 Feb 2019 19:32:48 +0200 Subject: [PATCH] Create LoadJSONFileAndEnv config method for convenience License: MIT Signed-off-by: Robert Ignat --- cmd/ipfs-cluster-service/daemon.go | 2 +- cmd/ipfs-cluster-service/main.go | 2 +- cmd/ipfs-cluster-service/state.go | 6 +++--- config/config.go | 11 +++++++++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/ipfs-cluster-service/daemon.go b/cmd/ipfs-cluster-service/daemon.go index efa2f0d1..5dab00b0 100644 --- a/cmd/ipfs-cluster-service/daemon.go +++ b/cmd/ipfs-cluster-service/daemon.go @@ -69,7 +69,7 @@ func daemon(c *cli.Context) error { // always wait for configuration to be saved defer cfgMgr.Shutdown() - err = cfgMgr.LoadJSONFromFile(configPath) + err = cfgMgr.LoadJSONFileAndEnv(configPath) checkErr("loading configuration", err) if c.Bool("stats") { diff --git a/cmd/ipfs-cluster-service/main.go b/cmd/ipfs-cluster-service/main.go index 5c95fb53..0d82358c 100644 --- a/cmd/ipfs-cluster-service/main.go +++ b/cmd/ipfs-cluster-service/main.go @@ -442,7 +442,7 @@ the mth data folder (m currently defaults to 5) } cfgMgr, cfgs := makeConfigs() - err = cfgMgr.LoadJSONFromFile(configPath) + err = cfgMgr.LoadJSONFileAndEnv(configPath) checkErr("reading configuration", err) err = cleanupState(cfgs.consensusCfg) diff --git a/cmd/ipfs-cluster-service/state.go b/cmd/ipfs-cluster-service/state.go index 18c19d1a..9cdcc694 100644 --- a/cmd/ipfs-cluster-service/state.go +++ b/cmd/ipfs-cluster-service/state.go @@ -34,7 +34,7 @@ func upgrade(ctx context.Context) error { cfgMgr, cfgs := makeConfigs() - err = cfgMgr.LoadJSONFromFile(configPath) + err = cfgMgr.LoadJSONFileAndEnv(configPath) if err != nil { return err } @@ -65,7 +65,7 @@ func restoreStateFromDisk(ctx context.Context) (*mapstate.MapState, bool, error) cfgMgr, cfgs := makeConfigs() - err := cfgMgr.LoadJSONFromFile(configPath) + err := cfgMgr.LoadJSONFileAndEnv(configPath) if err != nil { return nil, false, err } @@ -108,7 +108,7 @@ func stateImport(ctx context.Context, r io.Reader) error { cfgMgr, cfgs := makeConfigs() - err := cfgMgr.LoadJSONFromFile(configPath) + err := cfgMgr.LoadJSONFileAndEnv(configPath) if err != nil { return err } diff --git a/config/config.go b/config/config.go index aa479bdf..5db06fe3 100644 --- a/config/config.go +++ b/config/config.go @@ -322,6 +322,17 @@ func (cfg *Manager) LoadJSONFromFile(path string) error { return err } +// LoadJSONFileAndEnv calls LoadJSONFromFile followed by ApplyEnvVars, +// reading and parsing a Configuration file and then overriding fields +// with any values found in environment variables. +func (cfg *Manager) LoadJSONFileAndEnv(path string) error { + if err := cfg.LoadJSONFromFile(path); err != nil { + return err + } + + return cfg.ApplyEnvVars() +} + // LoadJSON parses configurations for all registered components, // In order to work, component configurations must have been registered // beforehand with RegisterComponent.