| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package pathutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
func DirsWithFile(root, fileName string, recurse bool) ([]string, error) { |
| 15 |
var res []string |
| 16 |
if !recurse { |
| 17 |
|
| 18 |
|
| 19 |
_, err := os.Stat(filepath.Join(root, fileName)) |
| 20 |
if !os.IsNotExist(err) { |
| 21 |
res = append(res, filepath.Clean(root)) |
| 22 |
} |
| 23 |
return res, nil |
| 24 |
} |
| 25 |
err := filepath.Walk(root, |
| 26 |
func(path string, info os.FileInfo, err error) error { |
| 27 |
if err != nil { |
| 28 |
return err |
| 29 |
} |
| 30 |
if filepath.Base(path) == fileName { |
| 31 |
res = append(res, filepath.Dir(path)) |
| 32 |
} |
| 33 |
return nil |
| 34 |
}) |
| 35 |
if err != nil { |
| 36 |
return res, err |
| 37 |
} |
| 38 |
return res, nil |
| 39 |
} |