List and set setters in folders recursively

This commit is contained in:
Phani Teja Marupaka
2020-07-20 00:50:04 -07:00
parent 45eed23b26
commit 108195185f
9 changed files with 153 additions and 60 deletions

View File

@@ -0,0 +1,35 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package pathutil
import (
"os"
"path/filepath"
"strings"
"sigs.k8s.io/kustomize/kyaml/errors"
)
// SubDirsWithFile takes the root directory path and returns all the paths of
// sub-directories which contain file with input fileName including itself
func SubDirsWithFile(root, fileName string) ([]string, error) {
var res []string
err := filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, fileName) {
if root == "." {
path = root + "/" + path
}
res = append(res, path)
}
return nil
})
if err != nil {
return res, errors.Wrap(err)
}
return res, nil
}