feat: add locateExecutable to finds plugin in hrp executable dir

This commit is contained in:
lilong.129
2023-08-19 15:17:12 +08:00
parent acfbe1dc84
commit a4cb07a854
2 changed files with 26 additions and 6 deletions

View File

@@ -1 +1 @@
v4.3.6-beta
v4.3.6-2308191518

View File

@@ -120,14 +120,15 @@ func locatePlugin(path string) (pluginPath string, err error) {
return
}
return "", fmt.Errorf("plugin file not found")
return "", errors.New("plugin file not found")
}
// locateFile searches destFile upward recursively until system root dir
func locateFile(startPath string, destFile string) (string, error) {
// if not found, then searches in hrp executable dir
func locateFile(startPath string, destFile string) (pluginPath string, err error) {
stat, err := os.Stat(startPath)
if os.IsNotExist(err) {
return "", err
return "", errors.Wrap(err, "start path not exists")
}
var startDir string
@@ -139,7 +140,7 @@ func locateFile(startPath string, destFile string) (string, error) {
startDir, _ = filepath.Abs(startDir)
// convention over configuration
pluginPath := filepath.Join(startDir, destFile)
pluginPath = filepath.Join(startDir, destFile)
if _, err := os.Stat(pluginPath); err == nil {
return pluginPath, nil
}
@@ -147,12 +148,31 @@ func locateFile(startPath string, destFile string) (string, error) {
// system root dir
parentDir, _ := filepath.Abs(filepath.Dir(startDir))
if parentDir == startDir {
return "", fmt.Errorf("searched to system root dir, plugin file not found")
if pluginPath, err = locateExecutable(destFile); err == nil {
return
}
return "", errors.New("searched to system root dir, plugin file not found")
}
return locateFile(parentDir, destFile)
}
// locateExecutable finds destFile in hrp executable dir
func locateExecutable(destFile string) (string, error) {
exePath, err := os.Executable()
if err != nil {
return "", errors.Wrap(err, "get hrp executable failed")
}
exeDir := filepath.Dir(exePath)
pluginPath := filepath.Join(exeDir, destFile)
if _, err := os.Stat(pluginPath); err == nil {
return pluginPath, nil
}
return "", errors.New("plugin file not found in hrp executable dir")
}
func GetProjectRootDirPath(path string) (rootDir string, err error) {
pluginPath, err := locatePlugin(path)
if err == nil {