feat: add locateExecutable to finds plugin in hrp executable dir

This commit is contained in:
lilong.129
2023-08-19 15:19:24 +08:00
parent acfbe1dc84
commit a4cb07a854
2 changed files with 26 additions and 6 deletions
+1 -1
View File
@@ -1 +1 @@
v4.3.6-beta v4.3.6-2308191518
+25 -5
View File
@@ -120,14 +120,15 @@ func locatePlugin(path string) (pluginPath string, err error) {
return return
} }
return "", fmt.Errorf("plugin file not found") return "", errors.New("plugin file not found")
} }
// locateFile searches destFile upward recursively until system root dir // 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) stat, err := os.Stat(startPath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return "", err return "", errors.Wrap(err, "start path not exists")
} }
var startDir string var startDir string
@@ -139,7 +140,7 @@ func locateFile(startPath string, destFile string) (string, error) {
startDir, _ = filepath.Abs(startDir) startDir, _ = filepath.Abs(startDir)
// convention over configuration // convention over configuration
pluginPath := filepath.Join(startDir, destFile) pluginPath = filepath.Join(startDir, destFile)
if _, err := os.Stat(pluginPath); err == nil { if _, err := os.Stat(pluginPath); err == nil {
return pluginPath, nil return pluginPath, nil
} }
@@ -147,12 +148,31 @@ func locateFile(startPath string, destFile string) (string, error) {
// system root dir // system root dir
parentDir, _ := filepath.Abs(filepath.Dir(startDir)) parentDir, _ := filepath.Abs(filepath.Dir(startDir))
if parentDir == 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) 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) { func GetProjectRootDirPath(path string) (rootDir string, err error) {
pluginPath, err := locatePlugin(path) pluginPath, err := locatePlugin(path)
if err == nil { if err == nil {