diff options
author | Chris <ccbrown112@gmail.com> | 2017-08-20 23:47:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-20 23:47:14 -0500 |
commit | 1222e6cd4172657143202882e7189274352e2cd5 (patch) | |
tree | 1ac334164ae64366d960e3ba2b56df8849ec07af /utils | |
parent | 4a8afebcdb8e8c88b4191d68cb1b5712d12cee9c (diff) | |
download | chat-1222e6cd4172657143202882e7189274352e2cd5.tar.gz chat-1222e6cd4172657143202882e7189274352e2cd5.tar.bz2 chat-1222e6cd4172657143202882e7189274352e2cd5.zip |
make config.json play nicely with version control (#7221)
Diffstat (limited to 'utils')
-rw-r--r-- | utils/config.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/utils/config.go b/utils/config.go index 36474c921..aa5b50146 100644 --- a/utils/config.go +++ b/utils/config.go @@ -7,6 +7,7 @@ import ( "crypto/md5" "encoding/json" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -309,6 +310,22 @@ func LoadConfig(fileName string) { configReadErr := viper.ReadInConfig() if configReadErr != nil { + if _, ok := configReadErr.(viper.ConfigFileNotFoundError); ok { + // In case of a file-not-found error, try to copy default.json if it's present. + defaultPath := FindConfigFile("default.json") + if src, err := os.Open(defaultPath); err == nil { + if dest, err := os.OpenFile(filepath.Join(filepath.Dir(defaultPath), "config.json"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600); err == nil { + if _, err := io.Copy(dest, src); err == nil { + configReadErr = viper.ReadInConfig() + } + dest.Close() + } + src.Close() + } + } + } + + if configReadErr != nil { errMsg := T("utils.config.load_config.opening.panic", map[string]interface{}{"Filename": fileName, "Error": configReadErr.Error()}) fmt.Fprintln(os.Stderr, errMsg) os.Exit(1) |