109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
// MIT License
|
|
//
|
|
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package howmuch
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const (
|
|
defaultConfigName = "howmuch"
|
|
homeConfigFolder = ".howmuch"
|
|
envPrevfix = "HOWMUCH"
|
|
configType = "yaml"
|
|
)
|
|
|
|
func defaultConfig() {
|
|
viper.SetDefault("dev-mode", true)
|
|
|
|
// web
|
|
viper.SetDefault("web.addr", ":8000")
|
|
viper.SetDefault("web.shutdown-timeout", "10")
|
|
|
|
// db
|
|
viper.SetDefault("db.host", "localhost")
|
|
viper.SetDefault("db.port", 5432)
|
|
viper.SetDefault("db.username", "postgres")
|
|
viper.SetDefault("db.password", "example")
|
|
viper.SetDefault("db.database", "howmuch")
|
|
viper.SetDefault("db.sslmode", "disable")
|
|
}
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
func initConfig() {
|
|
defaultConfig()
|
|
if cfgFile != "" {
|
|
// Use config file from the flag.
|
|
viper.SetConfigFile(cfgFile)
|
|
} else {
|
|
home, err := os.UserHomeDir()
|
|
cobra.CheckErr(err)
|
|
|
|
viper.AddConfigPath(filepath.Join(home, homeConfigFolder))
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.SetConfigType(configType)
|
|
viper.SetConfigName(defaultConfigName)
|
|
}
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
viper.SetEnvPrefix(envPrevfix)
|
|
|
|
// replace . - to _ in the envvar
|
|
replacer := strings.NewReplacer(".", "_", "-", "_")
|
|
viper.SetEnvKeyReplacer(replacer)
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
log.ErrorLog("Failed to read viper configuration file, use default config", "err", err)
|
|
return
|
|
}
|
|
|
|
// watching reloading conf
|
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
|
log.InfoLog("Config file changed:", e.Name)
|
|
})
|
|
viper.WatchConfig()
|
|
|
|
log.DebugLog("Using config file", "file", viper.ConfigFileUsed())
|
|
}
|
|
|
|
// logOptions set log options from the configs read by viper.
|
|
func logOptions() *log.Options {
|
|
return &log.Options{
|
|
Level: viper.GetString("log.level"),
|
|
Development: viper.GetBool("dev-mode"),
|
|
DisableCaller: viper.GetBool("log.disable-caller"),
|
|
DisableStacktrace: viper.GetBool("log.disable-stacktrace"),
|
|
Format: viper.GetString("log.format"),
|
|
OutputPaths: viper.GetStringSlice("log.output-paths"),
|
|
}
|
|
}
|