From 419c97cd4a2f48783d1b95eb5082d513dc774241 Mon Sep 17 00:00:00 2001 From: "lin.huang" Date: Sat, 4 Nov 2023 16:50:02 +0800 Subject: [PATCH] fix:config path logic --- pkg/common/config/parse.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index fee5efbe9..2a877d69b 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -35,6 +35,16 @@ const ( DefaultFolderPath = "../config/" ) +// return absolude path join ../config/, this is k8s container config path +func GetDefaultConfigPath() string { + b, err := filepath.Abs(os.Args[0]) + if err != nil { + fmt.Println("filepath.Abs error,err=", err) + return "" + } + return filepath.Join(filepath.Dir(b), "../config/") +} + // getProjectRoot returns the absolute path of the project root directory func GetProjectRoot() string { b, _ := filepath.Abs(os.Args[0]) @@ -65,9 +75,11 @@ func initConfig(config interface{}, configName, configFolderPath string) error { _, err := os.Stat(configFolderPath) if err != nil { if !os.IsNotExist(err) { + fmt.Println("stat config path error:", err.Error()) return fmt.Errorf("stat config path error: %w", err) } configFolderPath = filepath.Join(GetProjectRoot(), "config", configName) + fmt.Println("flag's path,enviment's path,default path all is not exist,using project path:", configFolderPath) } data, err := os.ReadFile(configFolderPath) if err != nil { @@ -86,7 +98,7 @@ func InitConfig(configFolderPath string) error { if envConfigPath != "" { configFolderPath = envConfigPath } else { - configFolderPath = DefaultFolderPath + configFolderPath = GetDefaultConfigPath() } }