You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

findup.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package gofindup
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. type readDir func(string) ([]os.FileInfo, error)
  8. var defaultReadDir readDir = ioutil.ReadDir
  9. func hasFile(name, dir string, readdir readDir) (bool, error) {
  10. files, err := readdir(dir)
  11. if err != nil {
  12. return false, err
  13. }
  14. for _, f := range files {
  15. if name == f.Name() {
  16. return true, nil
  17. }
  18. }
  19. return false, nil
  20. }
  21. func findupFrom(name, dir string, readdir readDir) (string, error) {
  22. for {
  23. found, err := hasFile(name, dir, readdir)
  24. if err != nil {
  25. return "", err
  26. }
  27. if found {
  28. return filepath.Join(dir, name), nil
  29. }
  30. parent := filepath.Dir(dir)
  31. if parent == dir {
  32. return "", nil
  33. }
  34. dir = parent
  35. }
  36. }
  37. func findup(name string, readdir readDir) (string, error) {
  38. cwd, err := os.Getwd()
  39. if err != nil {
  40. return "", err
  41. }
  42. return findupFrom(name, cwd, readdir)
  43. }
  44. // Recursively find a file by walking up parents in the file tree
  45. // starting from a specific directory.
  46. func FindupFrom(name, dir string) (string, error) {
  47. return findupFrom(name, dir, defaultReadDir)
  48. }
  49. // Recursively find a file by walking up parents in the file tree
  50. // starting from the current working directory.
  51. func Findup(name string) (string, error) {
  52. return findup(name, defaultReadDir)
  53. }