go-urlsh/internal/config/file.go

30 lines
775 B
Go

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")
}
}