diff --git a/internal/fsutil/filepath.go b/internal/fsutil/filepath.go new file mode 100644 index 0000000..e50abd0 --- /dev/null +++ b/internal/fsutil/filepath.go @@ -0,0 +1,33 @@ +package fsutil + +import ( + "os" + "path/filepath" +) + +// FindAncestorPath searches for a file or directory with the given name (s) +// in the current directory and all parent directories. +// It returns the full path if found, or an empty string if not found. +func FindAncestorPath(s string) string { + currentDir, err := os.Getwd() + if err != nil { + return "" + } + + for { + fullPath := filepath.Join(currentDir, s) + if _, err := os.Stat(fullPath); err == nil { + return fullPath + } + + // Move to the parent directory + parentDir := filepath.Dir(currentDir) + if parentDir == currentDir { + // We've reached the root directory + break + } + currentDir = parentDir + } + + return "" +}