Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
goalongway committed Sep 26, 2024
1 parent b4c9320 commit 499ab80
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions applications/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bsquared.network/b2-message-channel-applications/internal/enums"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/viper"
"strconv"
Expand Down Expand Up @@ -62,11 +63,13 @@ type Particle struct {
ProjectKey string
}

func LoadConfig(fileName string) AppConfig {
func LoadConfig(input string) AppConfig {
path, filename, suffix := parsePath(input)
fmt.Printf("path: %s\n filename: %s\n suffix: %s\n", path, filename, suffix)
v := viper.New()
v.SetConfigName(fileName)
v.AddConfigPath("./config")
v.SetConfigType("yaml")
v.SetConfigName(filename)
v.AddConfigPath(path)
v.SetConfigType(suffix)
v.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
v.SetEnvKeyReplacer(replacer)
Expand Down Expand Up @@ -103,3 +106,21 @@ func ParseBridges(input string) (map[int64]string, error) {
}
return bridges, nil
}

func parsePath(input string) (string, string, string) {
var path, filename, suffix = ".", "config", "yaml"
suffix_index := strings.LastIndex(input, ".")
path_index := strings.LastIndex(input, "/")
if path_index < suffix_index && suffix_index > -1 {
suffix = input[suffix_index+1:]
}
if path_index > -1 {
path = input[:path_index]
}
if path_index > -1 && suffix_index > -1 && path_index < suffix_index {
filename = input[path_index+1 : suffix_index]
} else if path_index > -1 {
filename = input[path_index+1:]
}
return path, filename, suffix
}

0 comments on commit 499ab80

Please sign in to comment.