Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the flag parse #260

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading