feat: add -help, simplified config loading

This commit is contained in:
Deluan 2020-01-26 16:21:07 -05:00
parent cc58642185
commit 828dc8f0f4
2 changed files with 12 additions and 47 deletions

View file

@ -1,6 +1,7 @@
package conf package conf
import ( import (
"flag"
"os" "os"
"github.com/deluan/navidrome/consts" "github.com/deluan/navidrome/consts"
@ -20,62 +21,26 @@ type nd struct {
DisableDownsampling bool `default:"false"` DisableDownsampling bool `default:"false"`
DownsampleCommand string `default:"ffmpeg -i %s -map 0:0 -b:a %bk -v 0 -f mp3 -"` DownsampleCommand string `default:"ffmpeg -i %s -map 0:0 -b:a %bk -v 0 -f mp3 -"`
ProbeCommand string `default:"ffmpeg %s -f ffmetadata"` ProbeCommand string `default:"ffmpeg %s -f ffmetadata"`
PlsIgnoreFolders bool `default:"true"`
PlsIgnoredPatterns string `default:"^iCloud;\\~"`
ScanInterval string `default:"1m"` ScanInterval string `default:"1m"`
// DevFlags. These are used to enable/disable debugging and incomplete features // DevFlags. These are used to enable/disable debugging and incomplete features
DevDisableAuthentication bool `default:"false"` DevDisableAuthentication bool `default:"false"`
DevDisableBanner bool `default:"false"` DevDisableBanner bool `default:"false"`
DevInitialPassword string `default:""`
} }
var Server *nd 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)
}
func LoadFromFile(tomlFile string) { func LoadFromFile(tomlFile string) {
l := &multiconfig.TOMLLoader{Path: tomlFile} m := multiconfig.NewWithPath(tomlFile)
err := l.Load(Server) err := m.Load(Server)
if err != nil { if err == flag.ErrHelp {
log.Error("Error loading configuration file", "file", tomlFile, err) os.Exit(1)
} }
log.SetLogLevelString(Server.LogLevel)
} }
func LoadFromLocalFile() { func Load() {
if _, err := os.Stat(consts.LocalConfigFile); err == nil { if _, err := os.Stat(consts.LocalConfigFile); err == nil {
LoadFromFile(consts.LocalConfigFile) LoadFromFile(consts.LocalConfigFile)
} }
} }
func Load() {
LoadFromLocalFile()
LoadFromEnv()
//LoadFromFlags()
log.SetLogLevelString(Server.LogLevel)
}
func init() {
Server = new(nd)
LoadFromTags()
}

View file

@ -5,12 +5,12 @@ import (
) )
func main() { func main() {
conf.Load()
if !conf.Server.DevDisableBanner { if !conf.Server.DevDisableBanner {
ShowBanner() ShowBanner()
} }
conf.Load()
a := CreateServer(conf.Server.MusicFolder) a := CreateServer(conf.Server.MusicFolder)
a.MountRouter("/rest", CreateSubsonicAPIRouter()) a.MountRouter("/rest", CreateSubsonicAPIRouter())
a.MountRouter("/app", CreateAppRouter("/app")) a.MountRouter("/app", CreateAppRouter("/app"))