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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package gofindup
  2. import (
  3. "path/filepath"
  4. "github.com/spf13/afero"
  5. )
  6. var fs = afero.NewOsFs()
  7. func findIn(name, dir string, fs afero.Fs) (bool, error) {
  8. files, err := afero.ReadDir(fs, dir)
  9. if err != nil {
  10. return false, err
  11. }
  12. for _, f := range files {
  13. if name == f.Name() {
  14. return true, nil
  15. }
  16. }
  17. return false, nil
  18. }
  19. func findupFrom(name, dir string, fs afero.Fs) (bool, error) {
  20. for {
  21. found, err := findIn(name, dir, fs)
  22. if err != nil {
  23. return false, err
  24. }
  25. if found {
  26. return true, nil
  27. }
  28. parent := filepath.Dir(dir)
  29. if parent == dir {
  30. return false, nil
  31. }
  32. dir = parent
  33. }
  34. }
  35. func findup(name string, fs afero.Fs) (bool, error) {
  36. return findupFrom(name, ".", fs)
  37. }
  38. // Recursively find a file by walking up parents in the file tree
  39. // starting from a specific directory.
  40. func FindupFrom(name, dir string) (bool, error) {
  41. return findupFrom(name, dir, fs)
  42. }
  43. // Recursively find a file by walking up parents in the file tree
  44. // starting from the current working directory.
  45. func Findup(name string) (bool, error) {
  46. return findup(name, fs)
  47. }