Skip to content

Commit

Permalink
Adds support for folder/* patterns in .fleetignore (#2617)
Browse files Browse the repository at this point in the history
As of now, the `folder/*` was not supported by Fleet.

This PR adds support for it, giving the option to ignore folders (not files with a given name).

For example:

This folder structure:

```
root
   dir1
     test
       file1
       file2
   file3
test
file4
```

with the following `.fleetignore` file:
```
test/*
```

would produce the following recource list:
```
root/file3
root/test
root/file4
```

`test` folder would be ignored but `test` file would not.

Signed-off-by: Xavi Garcia <[email protected]>
  • Loading branch information
0xavi0 authored Jul 18, 2024
1 parent 34063c4 commit 2e55012
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 8 deletions.
31 changes: 23 additions & 8 deletions internal/bundlereader/loaddirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,40 @@ type ignoreTree struct {
}

// isIgnored checks whether any path within xt matches path, and returns true if so.
func (xt *ignoreTree) isIgnored(path string) (bool, error) {
func (xt *ignoreTree) isIgnored(path string, info fs.DirEntry) (bool, error) {
steps := xt.findNode(path, false, nil)

for _, step := range steps {
for _, ignoredPath := range step.ignoredPaths {
toIgnore, err := filepath.Match(ignoredPath, filepath.Base(path))
if err != nil {
return false, err
}
if isAllFilesInDirPattern(ignoredPath) {
// ignores a folder
if info.IsDir() {
dirNameInPattern := strings.TrimSuffix(ignoredPath, "/*")
if dirNameInPattern == filepath.Base(path) {
return true, nil
}
}
} else {
toIgnore, err := filepath.Match(ignoredPath, filepath.Base(path))
if err != nil {
return false, err
}

if toIgnore {
return true, nil
if toIgnore {
return true, nil
}
}
}
}

return false, nil
}

func isAllFilesInDirPattern(path string) bool {
match, _ := regexp.MatchString("^.+/\\*", path)
return match
}

// addNode reads a `.fleetignore` file in dir's root and adds each of its entries to ignored paths for dir.
// Returns an error if a `.fleetignore` file exists for dir but reading it fails.
func (xt *ignoreTree) addNode(dir string) error {
Expand Down Expand Up @@ -260,7 +275,7 @@ func GetContent(ctx context.Context, base, source, version string, auth Auth, di
return err
}

ignore, err := ignoredPaths.isIgnored(path)
ignore, err := ignoredPaths.isIgnored(path, info)
if err != nil {
return err
}
Expand Down
201 changes: 201 additions & 0 deletions internal/bundlereader/loaddirectory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,207 @@ func TestGetContent(t *testing.T) {
"bar/something2.yaml": []byte("something2"),
},
},
{
name: "root .fleetignore contains folder/* entries",
directoryStructure: fsNode{
isDir: true,
name: "root-fleetignore-all-files-in-dir",
children: []fsNode{
{
name: "something.yaml",
contents: "foo",
},
{
name: ".fleetignore",
contents: "foo/*\n",
},
{
name: "foo",
isDir: true,
children: []fsNode{
{
name: "ignore-always.yaml",
contents: "will be ignored",
},
{
name: "something.yaml",
contents: "will be ignored",
},
},
},
{
name: "bar",
isDir: true,
children: []fsNode{
{
name: "something.yaml",
contents: "something",
},
{
name: "something2.yaml",
contents: "something2",
},
{
name: "foo",
isDir: true,
children: []fsNode{
{
name: "ignore.yaml",
contents: "will be ignored",
},
{
name: "ignore2.yaml",
contents: "will be ignored",
},
{
name: "something.yaml",
contents: "will be ignored",
},
},
},
},
},
},
},
expectedFiles: map[string][]byte{
"something.yaml": []byte("foo"),
"bar/something.yaml": []byte("something"),
"bar/something2.yaml": []byte("something2"),
},
},
{
name: "non root .fleetignore contains folder/* entries",
directoryStructure: fsNode{
isDir: true,
name: "non-root-fleetignore-all-files-in-dir",
children: []fsNode{
{
name: "something.yaml",
contents: "foo",
},
{
name: "foo",
isDir: true,
children: []fsNode{
{
name: "something1.yaml",
contents: "something1",
},
{
name: "something2.yaml",
contents: "something2",
},
},
},
{
name: "bar",
isDir: true,
children: []fsNode{
{
name: "something.yaml",
contents: "something",
},
{
name: "something2.yaml",
contents: "something2",
},
{
name: ".fleetignore",
contents: "foo/*\n",
},
{
name: "foo",
isDir: true,
children: []fsNode{
{
name: "ignore.yaml",
contents: "will be ignored",
},
{
name: "ignore2.yaml",
contents: "will be ignored",
},
{
name: "something.yaml",
contents: "will be ignored",
},
},
},
},
},
},
},
expectedFiles: map[string][]byte{
"something.yaml": []byte("foo"),
"foo/something1.yaml": []byte("something1"),
"foo/something2.yaml": []byte("something2"),
"bar/something.yaml": []byte("something"),
"bar/something2.yaml": []byte("something2"),
},
},
{
name: ".fleetignore contains folder/* entry does not apply to files",
directoryStructure: fsNode{
isDir: true,
name: "fleetignore-all-files-in-dir-does-not-apply-to-files",
children: []fsNode{
{
name: "something.yaml",
contents: "foo",
},
{
name: ".fleetignore",
contents: "foo/*\n",
},
{
name: "foo",
contents: "everybody was a kung-foo fighting",
},
{
name: "bar",
isDir: true,
children: []fsNode{
{
name: "something.yaml",
contents: "something",
},
{
name: "something2.yaml",
contents: "something2",
},
{
name: ".fleetignore",
contents: "foo/*\n",
},
{
name: "foo",
isDir: true,
children: []fsNode{
{
name: "ignore.yaml",
contents: "will be ignored",
},
{
name: "ignore2.yaml",
contents: "will be ignored",
},
{
name: "something.yaml",
contents: "will be ignored",
},
},
},
},
},
},
},
expectedFiles: map[string][]byte{
"something.yaml": []byte("foo"),
"foo": []byte("everybody was a kung-foo fighting"),
"bar/something.yaml": []byte("something"),
"bar/something2.yaml": []byte("something2"),
},
},
}

base, err := os.MkdirTemp("", "test-fleet")
Expand Down

0 comments on commit 2e55012

Please sign in to comment.