Last active
August 29, 2015 14:15
-
-
Save jgoulah/037a942ad5632a34ccf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (cfg *configuration) Load() error { | |
paths := make([]string, 0) | |
fdir := filepath.Dir(pathToThisFile()) | |
config := []string{fmt.Sprintf("%s/../config.cfg", fdir)} | |
paths = findConfig(config, paths) | |
// we look in multiple places because in production we have the secrets deployed out the the /etc location | |
// instead of shipping with the app, this allows us to keep production secrets more secure | |
secrets_config := []string{fmt.Sprintf("%s/../config-secrets.cfg", fdir), "/etc/myapp/config-secrets.cfg"} | |
paths = findConfig(secrets_config, paths) | |
cfg.mtx.Lock() | |
defer cfg.mtx.Unlock() | |
for _, p := range paths { | |
err := gcfg.ReadFileInto(cfg, p) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
// the caveat here is this assumes the binary is run from the same place its built | |
// see the full example for one workaround https://gist.github.com/jgoulah/9a5009a34d7ce9f872a8 | |
func pathToThisFile() string { | |
_, file, _, _ := runtime.Caller(0) | |
return file | |
} | |
func findConfig(locations []string, paths []string) []string { | |
for _, c := range locations { | |
if _, err := os.Stat(c); err == nil { | |
paths = append(paths, c) | |
} | |
} | |
return paths | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment