locateFile searches destFile upward recursively until system root dir if not found, then searches in hrp executable dir
(startPath string, destFile string)
| 122 | // locateFile searches destFile upward recursively until system root dir |
| 123 | // if not found, then searches in hrp executable dir |
| 124 | func locateFile(startPath string, destFile string) (pluginPath string, err error) { |
| 125 | stat, err := os.Stat(startPath) |
| 126 | if os.IsNotExist(err) { |
| 127 | return "", errors.Wrap(err, "start path not exists") |
| 128 | } |
| 129 | |
| 130 | var startDir string |
| 131 | if stat.IsDir() { |
| 132 | startDir = startPath |
| 133 | } else { |
| 134 | startDir = filepath.Dir(startPath) |
| 135 | } |
| 136 | startDir, _ = filepath.Abs(startDir) |
| 137 | |
| 138 | // convention over configuration |
| 139 | pluginPath = filepath.Join(startDir, destFile) |
| 140 | if _, err := os.Stat(pluginPath); err == nil { |
| 141 | return pluginPath, nil |
| 142 | } |
| 143 | |
| 144 | // system root dir |
| 145 | parentDir, _ := filepath.Abs(filepath.Dir(startDir)) |
| 146 | if parentDir == startDir { |
| 147 | if pluginPath, err = locateExecutable(destFile); err == nil { |
| 148 | return |
| 149 | } |
| 150 | return "", errors.New("searched to system root dir, plugin file not found") |
| 151 | } |
| 152 | |
| 153 | return locateFile(parentDir, destFile) |
| 154 | } |
| 155 | |
| 156 | // locateExecutable finds destFile in hrp executable dir |
| 157 | func locateExecutable(destFile string) (string, error) { |