Files
rph/utils/utils.go
Squibid 2a22f41fac feat(vendordep): add vendordep management
- Add vendordep add
- Add vendordep remove
- Add vendordep list
- Refactor downloading
2025-10-17 14:15:37 -04:00

42 lines
1.0 KiB
Go

package utils
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
)
func FindEntryDirInParents(startPath string, lookingFor string) (string, error) {
absPath, err := filepath.Abs(startPath)
if err != nil {
slog.Error("Unable to get the absolute path", "path", startPath, "error", err)
return "", err
}
// Find our depth so we don't have to do more checking than necessary
components := strings.Split(filepath.Clean(absPath), string(os.PathSeparator))
// Start searching from the current directory upwards
currentPath := absPath
for i := len(components); i > 0; i-- {
lookingForPath := filepath.Join(currentPath, lookingFor)
_, err := os.Stat(lookingForPath)
if err == nil {
realLookingForPath, err := filepath.Abs(currentPath)
if err != nil {
slog.Error("Unable to get the absolute path", "path", lookingForPath, "error", err)
return "", err
}
return realLookingForPath, nil
}
// Go up one directory level
currentPath = filepath.Dir(currentPath)
}
return "", fmt.Errorf(lookingFor + " not found")
}