fix: only try to load config file if it exists, or else it would not load any config at all...

This commit is contained in:
Deluan 2020-01-26 17:09:08 -05:00
parent f9db80c409
commit 7c2728aadc
1 changed files with 39 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
@ -33,12 +34,48 @@ type nd struct {
var Server = &nd{}
func NewWithPath(path string) *multiconfig.DefaultLoader {
loaders := []multiconfig.Loader{}
// Read default values defined via tag fields "default"
loaders = append(loaders, &multiconfig.TagLoader{})
if _, err := os.Stat(consts.LocalConfigFile); err == nil {
if strings.HasSuffix(path, "toml") {
loaders = append(loaders, &multiconfig.TOMLLoader{Path: path})
}
if strings.HasSuffix(path, "json") {
loaders = append(loaders, &multiconfig.JSONLoader{Path: path})
}
if strings.HasSuffix(path, "yml") || strings.HasSuffix(path, "yaml") {
loaders = append(loaders, &multiconfig.YAMLLoader{Path: path})
}
}
e := &multiconfig.EnvironmentLoader{}
f := &multiconfig.FlagLoader{}
loaders = append(loaders, e, f)
loader := multiconfig.MultiLoader(loaders...)
d := &multiconfig.DefaultLoader{}
d.Loader = loader
d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{})
return d
}
func LoadFromFile(confFile string) {
m := multiconfig.NewWithPath(confFile)
m := NewWithPath(confFile)
err := m.Load(Server)
if err == flag.ErrHelp {
os.Exit(1)
}
if err != nil {
fmt.Printf("Error trying to load config '%s'. Error: %v", confFile, err)
os.Exit(2)
}
if Server.DbPath == "" {
Server.DbPath = filepath.Join(Server.DataFolder, "navidrome.db")
}
@ -47,7 +84,5 @@ func LoadFromFile(confFile string) {
}
func Load() {
if _, err := os.Stat(consts.LocalConfigFile); err == nil {
LoadFromFile(consts.LocalConfigFile)
}
LoadFromFile(consts.LocalConfigFile)
}