Refactor and clean up

This commit is contained in:
Deluan 2020-07-02 17:21:25 -04:00
parent d2e4cade62
commit a65c9bbb16
5 changed files with 44 additions and 51 deletions

View file

@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)
type nd struct {
type configOptions struct {
ConfigFile string
Port int
MusicFolder string
@ -36,11 +36,9 @@ type nd struct {
DevAutoCreateAdminPassword string
}
var Server = &nd{}
var Server = &configOptions{}
func LoadFromFile(confFile string) {
// Use config file from the flag.
SetDefaults()
viper.SetConfigFile(confFile)
Load()
}
@ -61,7 +59,7 @@ func Load() {
log.Debug("Loaded configuration", "file", Server.ConfigFile, "config", fmt.Sprintf("%#v", Server))
}
func SetDefaults() {
func init() {
viper.SetDefault("musicfolder", "./music")
viper.SetDefault("datafolder", "./")
viper.SetDefault("loglevel", "info")
@ -85,3 +83,20 @@ func SetDefaults() {
viper.SetDefault("devlogsourceline", false)
viper.SetDefault("devautocreateadminpassword", "")
}
func InitConfig(cfgFile string) error {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in local directory with name "navidrome" (without extension).
viper.AddConfigPath(".")
viper.SetConfigName("navidrome")
}
viper.BindEnv("port")
viper.SetEnvPrefix("ND")
viper.AutomaticEnv()
return viper.ReadInConfig()
}