Debian Codegraph @

kyaml/pathutil/pathutil.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pathutil
5
6 import (
7 "os"
8 "path/filepath"
9 )
10
11 // DirsWithFile takes the root directory path and returns all the paths of
12 // sub-directories(including itself) which contain file with input fileName
13 // at top level if recurse is true
14 func DirsWithFile(root, fileName string, recurse bool) ([]string, error) {
15 var res []string
16 if !recurse {
17 // check if the file with fileName is present in root and return it
18 // else return empty list
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 }