doublestar icon indicating copy to clipboard operation
doublestar copied to clipboard

Question - Supporting negation cases

Open ansrivas opened this issue 4 years ago • 6 comments

Hello, Thank you for your work on this library.

I have a use case where I'd exclude files and include others. I skimmed through the test-cases as well but I couldn't find, hence the query.

Reproducing steps:

$ mkdir -p level1/level2/

$ touch level1/level2/{a.stderr.0,a.stdout.0,ignore.stderr.0,ignore.stdout.0,b.stderr.0,b.stdout.0}

$ ls level1/level2/!(ignore*).std* 
level1/level2/a.stderr.0  level1/level2/a.stdout.0  level1/level2/b.stderr.0  level1/level2/b.stdout.0

Corresponding go-snippet:

$ cat main.go
package main

import (
	"fmt"

	"github.com/bmatcuk/doublestar/v2"
)

func main() {
	matched, err := doublestar.Glob("level1/level2/!(ignore*).std* ")
	if err != nil {
		panic(err)
	}

        // So here we find no matched files
	if len(matched) == 0 {
		panic(fmt.Errorf("No matched files found"))
	}
	for _, match := range matched {
		fmt.Println(match)
	}
}

Is this by design or it could be a feature worth supporting in the library?

ansrivas avatar Nov 10 '20 14:11 ansrivas

Hi @ansrivas - doublestar doesn't support negation, but that could be an interesting feature. For the time being, you could accomplish it by using a very inclusive pattern with Glob() (such as level1/level2/*) and then use Match() to filter out results matching your negative pattern (level1/level2/ignore*.std*).

bmatcuk avatar Nov 10 '20 14:11 bmatcuk

Thank you for your response @bmatcuk . I also figured we could use that. Just for the background, promtail from grafana uses doublestar to support file-glob/match.

Seems I should open this as an issue upstream: https://github.com/grafana/loki/blob/b9534c65b357b04049b57350da68be12b950521d/pkg/promtail/targets/file/filetarget.go#L180

ansrivas avatar Nov 10 '20 14:11 ansrivas

@ansrivas, did you open an issue in upstream about using another Match to exclude results?

PS in the mean time, I've created an issue in upstream at https://github.com/grafana/loki/issues/3804

rgl avatar Jun 05 '21 06:06 rgl

Hi @rgl no, unfortunately, we maintained a local fork to work on it. Thanks for opening this upstream.

ansrivas avatar Jun 05 '21 08:06 ansrivas

@ansrivas Could you publish your local fork? Could you create a PR at grafana/loki?

michaelkebe avatar Aug 19 '21 18:08 michaelkebe