package config import ( "os" "path" "runtime" "strings" "velvettear/badger/internal/log" "velvettear/badger/internal/tools" "github.com/spf13/viper" ) // exported function(s) func Initialize() { timestamp := tools.LogTimestamp() configSpecified := false for index, arg := range os.Args { if arg == "-h" || arg == "--"+help { tools.PrintHelp() } if arg == "-c" || arg == "--"+config { viper.SetConfigFile(os.Args[index+1]) configSpecified = true } } setDefaults() if !configSpecified { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath("$HOME/.config/badger/") viper.AddConfigPath("$HOME/.config/") workingDirectory, error := os.Getwd() for error == nil && path.Base(workingDirectory) != "badger" { workingDirectory = path.Dir(workingDirectory) if workingDirectory == "/" { workingDirectory = "." break } } viper.AddConfigPath(workingDirectory) } error := viper.ReadInConfig() if configSpecified && error != nil { log.FatalTimed("encountered an error parsing specified config file '"+viper.ConfigFileUsed()+"'", timestamp, error.Error()) } tmp := viper.GetStringSlice(libraryFormats) for index, format := range tmp { if strings.HasPrefix(format, ".") { continue } tmp[index] = "." + format } viper.Set(libraryFormats, tmp) if viper.GetInt(concurrency) <= 0 { viper.Set(concurrency, runtime.NumCPU()) } viper.Set(databaseJournalMode, strings.ToLower(viper.GetString(databaseJournalMode))) prepareFilesystem() if Debug() { log.SetLogLevel(0) } log.DebugTimed("parsed config file '"+viper.ConfigFileUsed()+"'", timestamp) } // unexported function(s) func prepareFilesystem() { tmp := []string{ viper.GetString(homeDirectory), viper.GetString(libraryDirectory), viper.GetString(importDirectory), viper.GetString(duplicatesDirectoy), } for _, directory := range tmp { error := tools.MkDirs(directory) if error != nil { log.Fatal("encountered an error creating directory '"+directory+"'", error.Error()) } } // tmp = []string{} // for _, file := range tmp { // error := tools.CreateFile(file) // if error != nil { // log.Fatal("encountered an error creating file '"+file+"'", error.Error()) // } // } } func setDefaults() { home := path.Join(os.TempDir(), "badger") // set directory defaults viper.SetDefault(homeDirectory, home) viper.SetDefault(libraryDirectory, path.Join(home, "library")) viper.SetDefault(importDirectory, path.Join(home, "import")) viper.SetDefault(duplicatesDirectoy, path.Join(home, "duplicates")) // set database defaults viper.SetDefault(databaseFile, path.Join(home, "badger.sqlite")) viper.SetDefault(databaseInMemory, true) viper.SetDefault(databaseChunkSize, 1000) viper.SetDefault(databaseBusyTimeout, 10000) viper.SetDefault(databaseJournalMode, "off") // set library defaults viper.SetDefault(libraryFormats, []string{"flac", "mp3"}) viper.SetDefault(concurrency, runtime.NumCPU()) viper.SetDefault(libraryChangedetectionModified, true) viper.SetDefault(libraryChangedetectionSize, true) viper.SetDefault(libraryChangedetectionChecksum, false) viper.SetDefault(libraryDuplicatesAction, "log") viper.SetDefault(libraryDuplicatesFormatMismatch, true) viper.SetDefault(libraryDuplicatesUseFingerprint, true) viper.SetDefault(libraryDuplicatesFingerprintThreshold, 0.95) // set api defaults viper.SetDefault(apiListen, "localhost") viper.SetDefault(apiPort, 3333) // set other defaults viper.SetDefault(debug, false) viper.SetDefault(help, false) }