Skip to content

Commit

Permalink
get config ext by env if config file without ext
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoruijie committed May 24, 2023
1 parent e116cf8 commit 06ef69e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/constant/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ const (
EgoGovernorEnableConfig = "EGO_GOVERNOR_ENABLE_CONFIG"
// EgoLogEnableAddCaller when set to true, your log will show caller, default value is false
EgoLogEnableAddCaller = "EGO_LOG_ENABLE_ADD_CALLER"
// EgoDefaultConfigExt defines default config file extension, support ".toml",".yaml",".json",
// EgoDefaultConfigExt effective only the configuration file path without extension name
EgoDefaultConfigExt = "EGO_DEFAULT_CONFIG_EXT"
)
7 changes: 6 additions & 1 deletion core/econf/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"github.com/gotomicro/ego/core/constant"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,10 +50,14 @@ func (fp *fileDataSource) Parse(path string, watch bool) econf.ConfigType {

func extParser(configAddr string) econf.ConfigType {
ext := filepath.Ext(configAddr)
if ext == "" { // 如果配置文件没有扩展名,尝试从环境变量获取配置文件的扩展名
ext = os.Getenv(constant.EgoDefaultConfigExt)
}

switch ext {
case ".json":
return econf.ConfigTypeJSON
case ".toml", "":
case ".toml":
return econf.ConfigTypeToml
case ".yaml":
return econf.ConfigTypeYaml
Expand Down

0 comments on commit 06ef69e

Please sign in to comment.