Skip to content

Commit

Permalink
修复:环境变量无法获取的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Mar 21, 2024
1 parent 5ca878d commit f55648f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
21 changes: 18 additions & 3 deletions configure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *config) Get(key string) any {
func (c *config) GetSubNodes(key string) map[string]any {
// 这里需要倒序获取列表,利用后面覆盖前面的方式来获取
m := make(map[string]any)
// 先添加默认值
// 先加载默认值
prefixKey := key + "."
for k, v := range c.def {
if strings.HasPrefix(k, prefixKey) {
Expand All @@ -80,9 +80,13 @@ func (c *config) GetSubNodes(key string) map[string]any {

// 再添加yaml、环境变量
for i := len(c.configProvider) - 1; i >= 0; i-- {
subMap, exists := c.configProvider[i].GetSubNodes(key)
if exists {
if subMap, exists := c.configProvider[i].GetSubNodes(key); exists {
for k, v := range subMap {
// 尝试从之前的map中找到key(忽略大小写)
// 目的是以yaml的key为准
if c.configProvider[i].Name() == "env" {
k = lookupMapKeyIgnoreCase(m, k)
}
m[k] = v
}
}
Expand Down Expand Up @@ -128,3 +132,14 @@ func (c *config) GetSliceNodes(key string) []map[string]any {
}
return []map[string]any{}
}

// 尝试从之前的map中找到key(忽略大小写)
// 目的是以yaml的key为准
func lookupMapKeyIgnoreCase(m map[string]any, key string) string {
for k, _ := range m {
if strings.EqualFold(k, key) {
return k
}
}
return key
}
31 changes: 24 additions & 7 deletions configure/envConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ func NewEnvConfig() *envConfig {
return &envConfig{}
}

func (r *envConfig) Name() string {
return "env"
}

func (r *envConfig) LoadConfigure() error {
return nil
}

func (r *envConfig) Get(key string) (any, bool) {
key = r.replace(key)
val, exists := os.LookupEnv(key)
val, exists := lookupEnvIgnoreCase(key)
if exists {
return val, true
}
Expand All @@ -27,12 +31,12 @@ func (r *envConfig) Get(key string) (any, bool) {

func (r *envConfig) GetSubNodes(key string) (map[string]any, bool) {
m := make(map[string]any)
prefixKey := r.replace(key) + "_"
for _, env := range os.Environ() {
if strings.HasPrefix(env, prefixKey) {
index := strings.Index(env, "=")
k := env[len(prefixKey):index]
v := env[index+1:]
prefixKey := strings.ToLower(r.replace(key) + "_")
for _, e := range os.Environ() {
if strings.HasPrefix(strings.ToLower(e), prefixKey) {
index := strings.Index(e, "=")
k := e[len(prefixKey):index]
v := e[index+1:]
m[k] = v
}
}
Expand All @@ -45,3 +49,16 @@ func (r *envConfig) replace(key string) string {
key = strings.ReplaceAll(key, "]", "_")
return key
}

func lookupEnvIgnoreCase(key string) (string, bool) {
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if len(pair) == 2 {
envKey := pair[0]
if strings.EqualFold(envKey, key) {
return pair[1], true
}
}
}
return "", false
}
2 changes: 2 additions & 0 deletions configure/iConfigLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ type IConfigProvider interface {
Get(key string) (any, bool)
// GetSubNodes 获取所有子节点
GetSubNodes(key string) (map[string]any, bool)
// Name 提供者名称
Name() string
}
3 changes: 3 additions & 0 deletions configure/yamlConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func NewYamlConfig(configFile string) *yamlConfig {
configFile: configFile,
}
}
func (r *yamlConfig) Name() string {
return "yaml"
}

func (r *yamlConfig) LoadConfigure() error {
data, err := os.ReadFile(r.configFile)
Expand Down
15 changes: 10 additions & 5 deletions test/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ func TestConfigureGet(t *testing.T) {
assert.Len(t, configure.GetSliceNodes("aaa"), 0)
}

//func TestEnvConfig(t *testing.T) {
// fs.Initialize[modules.FarseerKernelModule]("unit test")
// _ = os.Setenv("Database_default", "aaa=bb")
// assert.Equal(t, "aaa=bb", configure.GetSubNodes("Database")["default"])
//}
func TestEnvConfig(t *testing.T) {
fs.Initialize[modules.FarseerKernelModule]("unit test")
_ = os.Setenv("Database_default", "aaa=bb")
assert.Equal(t, "aaa=bb", os.Getenv("Database_default"))
assert.Equal(t, "aaa=bb", configure.GetSubNodes("Database")["default"])

assert.Equal(t, os.Getenv("COMMAND_MODE"), configure.GetString("command_mode"))
nodes := configure.GetSubNodes("command")
assert.Equal(t, os.Getenv("COMMAND_MODE"), nodes["MODE"])
}

0 comments on commit f55648f

Please sign in to comment.