Skip to content

Commit

Permalink
Also add internal fsutil
Browse files Browse the repository at this point in the history
  • Loading branch information
gwillem committed Aug 30, 2024
1 parent 271cbf4 commit 1785335
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/fsutil/filepath.go
Original file line number Diff line number Diff line change
@@ -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 ""
}

0 comments on commit 1785335

Please sign in to comment.