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

inquiry on the skipNode functionality in regards to file name filtering. #53

Closed
zveinn opened this issue Aug 10, 2020 · 1 comment
Closed
Assignees
Labels

Comments

@zveinn
Copy link

zveinn commented Aug 10, 2020

Hello, I have been looking for a good package to walk directories for a while and I stumbled upon this one today. I really love the effort you have made into solving the issue with the path/filepath package provided by the go-team.

However, I have one small issue. I was trying to implement a file name filter and it kind of felt a bit awkward. Here is an example:

func main() {
	_ = godirwalk.Walk(".", &godirwalk.Options{
		Callback: func(osPathname string, de *godirwalk.Dirent) error {
			if strings.Contains(osPathname, ".git") {
				return errors.New("skipping: " + osPathname)
			}
			return nil
		},
	})
}

In the code sample above, I have implemented a word filter for the file path but if I keep the implementation like this, it will stop on the first math and not continue walking the directory.

However if I add an error callback like so:

func main() {
	_ = godirwalk.Walk("./../..", &godirwalk.Options{
		Callback: func(osPathname string, de *godirwalk.Dirent) error {
			if strings.Contains(osPathname, ".git") {
				return errors.New("skipping: " + osPathname)
			}
			return nil
		},
		ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
			return godirwalk.SkipNode
		},
	})
}

We get the proper functionality where the walk only ignores the directory I returned an error for.

This feels a bit awkward because now I am ignoring all errors in the error callback and I don´t want to do that by default. If I want to know which errors are filter(word match) errors I need to do another string check in the error callback function.

func main() {
	_ = godirwalk.Walk("./../..", &godirwalk.Options{
		Callback: func(osPathname string, de *godirwalk.Dirent) error {
			if strings.Contains(osPathname, ".git") {
				return errors.New("path filter")
			}
			return nil
		},
		ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
			if err.Error() == "path filter" {
				return godirwalk.SkipNode
			}
			return godirwalk.Halt
		},
	})
}

Is there possibly a way to implement a file name/path filter in a better way that I am not seeing ? and if not, can I then perhaps take a shot at it and make a PR ?

Regards,

@karrick karrick self-assigned this Aug 10, 2020
karrick pushed a commit that referenced this issue Aug 10, 2020
Ref: #53

Returning the special value `filepath.SkipDir` as an error token is
supported by `filepath.Walk` and by `godirwalk.Walk` to configure the
walk function to not descend further in the directory hierarchy of the
current file system entry.

This change updates the example find-fast program to demonstrate use
of this token error value.
@karrick
Copy link
Owner

karrick commented Aug 10, 2020

You raise an incredibly valid issue, namely being able to skip a node and its descendants as soon as you come across it in the original callback.

I have updated the find-fast example program to demonstrate how to tell the Walk function how to skip a file system entry, and to not descend into its children.

Please re-open this issue if I misunderstood your request, but I think I have addressed your concerns.

@karrick karrick closed this as completed Aug 10, 2020
karrick pushed a commit that referenced this issue Aug 10, 2020
Ref: #53

Returning the special value `filepath.SkipDir` as an error token is
supported by `filepath.Walk` and by `godirwalk.Walk` to configure the
walk function to not descend further in the directory hierarchy of the
current file system entry.

This change updates the example find-fast program to demonstrate use
of this token error value.
karrick pushed a commit that referenced this issue Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants