resource-scraper/internal/config/config.go

35 lines
752 B
Go

package config
import (
"fmt"
"log"
"path/filepath"
"github.com/spf13/viper"
)
func New() {
configFilePath := viper.GetString("config-file")
configDir := "./" + filepath.Dir(configFilePath)
viper.SetConfigName(filepath.Base(configFilePath))
viper.AddConfigPath(configDir)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
viper.SetDefault("ConfigDir", configDir)
scope := viper.GetString("scope")
if scope != "" {
viper.SetConfigName(scope)
viper.AddConfigPath(configDir)
err := viper.MergeInConfig()
if err != nil {
log.Fatalf("fatal error config file: %v", err)
return
}
}
}