Add support for .krmignore file

This commit is contained in:
Morten Torkildsen
2020-08-26 17:40:04 -07:00
parent e39a5adc00
commit 16bbc2d67e
8 changed files with 448 additions and 99 deletions

View File

@@ -184,8 +184,13 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
var operand ResourceNodeSlice
var pathRelativeTo string
r.PackagePath = filepath.Clean(r.PackagePath)
err := filepath.Walk(r.PackagePath, func(
var err error
ignoreFilesMatcher := &ignoreFilesMatcher{}
r.PackagePath, err = filepath.Abs(r.PackagePath)
if err != nil {
return nil, errors.Wrap(err)
}
err = filepath.Walk(r.PackagePath, func(
path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err)
@@ -194,9 +199,10 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
// is this the user specified path?
if path == r.PackagePath {
if info.IsDir() {
// skip the root package directory
// skip the root package directory, but check for a
// .krmignore file first.
pathRelativeTo = r.PackagePath
return nil
return ignoreFilesMatcher.readIgnoreFile(path)
}
// user specified path is a file rather than a directory.
@@ -206,9 +212,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
// check if we should skip the directory or file
if info.IsDir() {
return r.ShouldSkipDir(path)
return r.ShouldSkipDir(path, ignoreFilesMatcher)
}
if match, err := r.ShouldSkipFile(info); err != nil {
if match, err := r.ShouldSkipFile(path, ignoreFilesMatcher); err != nil {
return err
} else if !match {
// skip this file
@@ -251,10 +257,15 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode
}
// ShouldSkipFile returns true if the file should be skipped
func (r *LocalPackageReader) ShouldSkipFile(info os.FileInfo) (bool, error) {
func (r *LocalPackageReader) ShouldSkipFile(path string, matcher *ignoreFilesMatcher) (bool, error) {
// check if the file is covered by a .krmignore file.
if matcher.matchFile(path) {
return false, nil
}
// check if the files are in scope
for _, g := range r.MatchFilesGlob {
if match, err := filepath.Match(g, info.Name()); err != nil {
if match, err := filepath.Match(g, filepath.Base(path)); err != nil {
return false, errors.Wrap(err)
} else if match {
return true, nil
@@ -274,13 +285,22 @@ func (r *LocalPackageReader) initReaderAnnotations(path string, _ os.FileInfo) {
}
// ShouldSkipDir returns a filepath.SkipDir if the directory should be skipped
func (r *LocalPackageReader) ShouldSkipDir(path string) error {
func (r *LocalPackageReader) ShouldSkipDir(path string, matcher *ignoreFilesMatcher) error {
if r.PackageFileName == "" {
// If the folder is not a package, but covered by the .krmignore file,
// we skip it.
if matcher.matchDir(path) {
return filepath.SkipDir
}
return nil
}
// check if this is a subpackage
_, err := os.Stat(filepath.Join(path, r.PackageFileName))
if os.IsNotExist(err) {
// Skip the folder if it is covered by the .krmignore file.
if matcher.matchDir(path) {
return filepath.SkipDir
}
return nil
} else if err != nil {
return errors.Wrap(err)
@@ -288,5 +308,9 @@ func (r *LocalPackageReader) ShouldSkipDir(path string) error {
if !r.IncludeSubpackages {
return filepath.SkipDir
}
return nil
// We don't allow the .krmignore file in a package cause us to skip
// a subpackage. So if we have found a package file in the folder and
// we should include subpackages, we don't check the .krmignore file. We
// do however check whether the package contains a .krmignore file.
return matcher.readIgnoreFile(path)
}