From 828dc8f0f4ded74a3393b2246e9b2ec99e5b39d0 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sun, 26 Jan 2020 16:21:07 -0500 Subject: [PATCH] feat: add -help, simplified config loading --- conf/configuration.go | 55 ++++++++----------------------------------- main.go | 4 ++-- 2 files changed, 12 insertions(+), 47 deletions(-) diff --git a/conf/configuration.go b/conf/configuration.go index 87661f0f1..e4937dd82 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -1,6 +1,7 @@ package conf import ( + "flag" "os" "github.com/deluan/navidrome/consts" @@ -20,62 +21,26 @@ type nd struct { DisableDownsampling bool `default:"false"` DownsampleCommand string `default:"ffmpeg -i %s -map 0:0 -b:a %bk -v 0 -f mp3 -"` ProbeCommand string `default:"ffmpeg %s -f ffmetadata"` - PlsIgnoreFolders bool `default:"true"` - PlsIgnoredPatterns string `default:"^iCloud;\\~"` ScanInterval string `default:"1m"` // DevFlags. These are used to enable/disable debugging and incomplete features - DevDisableAuthentication bool `default:"false"` - DevDisableBanner bool `default:"false"` - DevInitialPassword string `default:""` + DevDisableAuthentication bool `default:"false"` + DevDisableBanner bool `default:"false"` } -var Server *nd - -func LoadFromFlags() { - l := &multiconfig.FlagLoader{} - l.Load(Server) -} - -func LoadFromEnv() { - port := os.Getenv("PORT") - if port != "" { - Server.Port = port - } - l := &multiconfig.EnvironmentLoader{} - err := l.Load(Server) - if err != nil { - log.Error("Error parsing configuration from environment") - } -} - -func LoadFromTags() { - l := &multiconfig.TagLoader{} - l.Load(Server) -} +var Server = &nd{} func LoadFromFile(tomlFile string) { - l := &multiconfig.TOMLLoader{Path: tomlFile} - err := l.Load(Server) - if err != nil { - log.Error("Error loading configuration file", "file", tomlFile, err) + m := multiconfig.NewWithPath(tomlFile) + err := m.Load(Server) + if err == flag.ErrHelp { + os.Exit(1) } + log.SetLogLevelString(Server.LogLevel) } -func LoadFromLocalFile() { +func Load() { if _, err := os.Stat(consts.LocalConfigFile); err == nil { LoadFromFile(consts.LocalConfigFile) } } - -func Load() { - LoadFromLocalFile() - LoadFromEnv() - //LoadFromFlags() - log.SetLogLevelString(Server.LogLevel) -} - -func init() { - Server = new(nd) - LoadFromTags() -} diff --git a/main.go b/main.go index 6f98fa1c8..9ccfbc247 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,12 @@ import ( ) func main() { - conf.Load() - if !conf.Server.DevDisableBanner { ShowBanner() } + conf.Load() + a := CreateServer(conf.Server.MusicFolder) a.MountRouter("/rest", CreateSubsonicAPIRouter()) a.MountRouter("/app", CreateAppRouter("/app"))