mcd/internal/config/config.go

42 lines
1.1 KiB
Go

package config
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func InitConfig(cfgFile string) func() {
return func () {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name "mcd.toml" (without extension).
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.SetConfigName("mcd")
}
viper.SetEnvPrefix("MCD")
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
viper.SetDefault("java.path", "/usr/bin/java")
viper.SetDefault("java.args", []string{"-Xms2G", "-Xmx3G"})
viper.SetDefault("server.jar", "./server.jar")
viper.SetDefault("gotify.enabled", false)
viper.SetDefault("gotify.url", "https://gotify.example.com/")
viper.SetDefault("gotify.token", "")
viper.SetDefault("gotify.join", true)
viper.SetDefault("gotify.leave", true)
viper.SetDefault("gotify.name", "mcd")
//Automatically reload config
viper.WatchConfig()
}
}