Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for folder/* patterns in .fleetignore #2617

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading