preset scraper added
This commit is contained in:
@@ -3,32 +3,62 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"git.amok.space/yevhen/resource-scraper/types/constant"
|
||||
)
|
||||
|
||||
func New() {
|
||||
configFilePath := viper.GetString("config-file")
|
||||
configDir := "./" + filepath.Dir(configFilePath)
|
||||
configFilePath := viper.GetString(constant.FlagConfigFile)
|
||||
configName := filepath.Base(configFilePath)
|
||||
|
||||
dir := filepath.Dir(configFilePath)
|
||||
if dir == configName && dir == "." {
|
||||
configName = "default"
|
||||
}
|
||||
|
||||
configDir, _ := filepath.Abs(dir)
|
||||
|
||||
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
|
||||
viper.SetConfigName(configName)
|
||||
err := viper.ReadInConfig()
|
||||
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
|
||||
viper.SetConfigName("secret")
|
||||
err = viper.MergeInConfig()
|
||||
if err != nil { // Handle errors reading the config file
|
||||
panic(fmt.Errorf("fatal error secret file: %w", err))
|
||||
}
|
||||
|
||||
slog.Info("using config", "path", filepath.Join(configDir, configName))
|
||||
viper.SetDefault(constant.CfgKeyConfigDir, configDir)
|
||||
|
||||
// Scopes validating
|
||||
scope := viper.GetString(constant.CfgKeyScopeEnable)
|
||||
scopesAllowed := viper.GetStringSlice("scope.allow")
|
||||
|
||||
if !slices.Contains(scopesAllowed, scope) {
|
||||
scope = viper.GetString("scope.default")
|
||||
|
||||
if scope == "" {
|
||||
scope = constant.ScopeInfo
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("scope", scope)
|
||||
|
||||
viper.SetConfigName(scope)
|
||||
viper.AddConfigPath(filepath.Join(configDir, "scope"))
|
||||
err = viper.MergeInConfig()
|
||||
if err == nil {
|
||||
viper.SetDefault(constant.CfgKeyScopeEnable, scope)
|
||||
} else {
|
||||
log.Fatalf("fatal error config file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user