Skip to content

Commit

Permalink
fix: fix the flag parse (#260)
Browse files Browse the repository at this point in the history
* fix:alter the config.yaml path

* fix:fix the const error

* fix:update the config.yaml path

* fix:update parse.go

* Update parse.go

add default path

* Update parse.go

* Update parse.go

* Update parse.go
  • Loading branch information
luhaoling authored Nov 8, 2023
1 parent 9d43247 commit dc74d65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 27 additions & 5 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,24 @@ func findConfigFile(paths []string) (string, error) {
return "", fmt.Errorf("configPath not found")
}

func CreateCatalogPath() []string {
func CreateCatalogPath(path string) []string {

path1 := filepath.Dir(path)
path1 = filepath.Dir(path1)
// the parent of binary file
pa1 := filepath.Join(path1, Constant.ConfigPath)
path2 := filepath.Dir(path1)
path2 = filepath.Dir(path2)
path2 = filepath.Dir(path2)
// the parent is _output
pa2 := filepath.Join(path2, Constant.ConfigPath)
path3 := filepath.Dir(path2)
// the parent is project(default)
pa3 := filepath.Join(path3, Constant.ConfigPath)

return []string{pa1, pa2,pa3}


return []string{Constant.ConfigPath1, Constant.ConfigPath2}
}

func findConfigPath(configFile string) (string, error) {
Expand All @@ -209,27 +224,34 @@ func findConfigPath(configFile string) (string, error) {
if _, err := findConfigFile([]string{configFile}); err != nil {
return "", errors.New("the configFile argument path is error")
}
fmt.Println("configfile:", configFile)
return configFile, nil
}

// Second, check for OPENIMCONFIG environment variable
//envConfigPath := os.Getenv(Constant.OpenIMConfig)
envConfigPath := os.Getenv(Constant.OpenIMConfig)
if envConfigPath != "" {
if _, err := findConfigFile([]string{envConfigPath}); err != nil {
return "", errors.New("the environment path config path is error")
}
return envConfigPath, nil
}

// Third, check the catalog to find the config.yaml
path = CreateCatalogPath()

p1, err := os.Executable()
if err != nil {
return "", err
}

path = CreateCatalogPath(p1)
pathFind, err := findConfigFile(path)
if err == nil {
return pathFind, nil
}

// Forth, use the Default path.
return Constant.Default, nil
return "", errors.New("the config.yaml path not found")
}

func FlagParse() (string, int, bool, bool, error) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import "github.com/OpenIMSDK/protocol/constant"

// config path
const (
ConfigPath1 = "../config/config.yaml"
ConfigPath2 = "../../../../../config/config.yaml"
ConfigPath = "/config/config.yaml"

OpenIMConfig = "OpenIMConfig" // environment variables
Default = "../../../config/config.yaml"
)

const (
Expand Down

0 comments on commit dc74d65

Please sign in to comment.