go-urlsh/internal/config/file.go

31 lines
775 B
Go
Raw Normal View History

2024-09-30 13:19:01 +02:00
package config
import (
"os"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)
// loadConfigFile reads the config file from given string OR
// from either $WorkDir/config.* or from /etc/go-urlsh/config.*
// Returns map[string]any with the specified config files.
// Panics if it can't read any config file.
func loadConfigFile(filepath string, logger *zerolog.Logger) {
if filepath == "" {
path, err := os.Executable()
if err != nil {
logger.Fatal().Err(err).Msg("error getting current working dir")
}
viper.SetConfigName("config")
viper.AddConfigPath("/etc/go-urlsh/")
viper.AddConfigPath(path)
} else {
viper.SetConfigFile(filepath)
}
err := viper.ReadInConfig()
if err != nil {
logger.Fatal().Err(err).Msg("failed to open config file")
}
}