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

Alternatives not working with slashes properly #25

Closed
karlism opened this issue Nov 26, 2019 · 9 comments
Closed

Alternatives not working with slashes properly #25

karlism opened this issue Nov 26, 2019 · 9 comments
Assignees
Labels

Comments

@karlism
Copy link

karlism commented Nov 26, 2019

Hello,

Alternatives in glob search patterns seem to not be working properly, when they contain slashes. According to documentation, it should be possible to escape special characters by using \, but it doesn't seem to work:

Steps to reproduce:

$ mkdir /tmp/test && touch /tmp/test/example{1,2}
$ ls /tmp/test
example1 example2
$ cat example.go
package main

import (
    "fmt"
    "github.com/bmatcuk/doublestar"
)

func main() {
    patterns := [6]string{
        "/tmp/test/*",
        "/tmp/test/{example1}",
        "/tmp/test/{example*}",
        "/tmp/test/{example1,example2}",
        "{/tmp/test/example*}",       // not working
        "{\\/tmp\\/test\\/example1}"} // not working
    for _, pattern := range patterns {
        fmt.Println("pattern:", pattern)
        matches, err := doublestar.Glob(pattern)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println(" ", matches, "\n")
        }
    }
}

$ go run example.go
pattern: /tmp/test/*
  [/tmp/test/example1 /tmp/test/example2] 

pattern: /tmp/test/{example1}
  [/tmp/test/example1] 

pattern: /tmp/test/{example*}
  [/tmp/test/example1 /tmp/test/example2] 

pattern: /tmp/test/{example1,example2}
  [/tmp/test/example1 /tmp/test/example2] 

pattern: {/tmp/test/example*}
syntax error in pattern
pattern: {\/tmp\/test\/example1}
  [] 

These patterns are working perfectly fine in both ksh and bash:

$ echo $0
ksh

$ ls /tmp/test/{example1,example2}
/tmp/test/example1  /tmp/test/example2

$ ls {/tmp/test/example1,/tmp/test/example2}
/tmp/test/example1  /tmp/test/example2

$ bash

$ echo $0
bash

$ ls /tmp/test/{example1,example2}
/tmp/test/example1  /tmp/test/example2

$ ls {/tmp/test/example1,/tmp/test/example2}
/tmp/test/example1  /tmp/test/example2

Being able to have full path (and slashes) in pattern is required to be able to find log files in different directories on the system (like /var/log & /opt/application/logs), more details about existing issue in Loki, which relies on doublestar, are available here.

Thanks!

@bmatcuk
Copy link
Owner

bmatcuk commented Nov 26, 2019

Oops, I totally responded to this in my head and meant to get to it right away, but got distracted! That definitely sounds like an issue; I'll take a look first chance I get. Thanks for the detailed report!

@bmatcuk bmatcuk self-assigned this Nov 26, 2019
@bmatcuk bmatcuk added the bug label Nov 26, 2019
@bmatcuk
Copy link
Owner

bmatcuk commented Nov 30, 2019

Ugh, I'm having some trouble implementing this... I think the best way to implement it would be a rewrite, which I'm trying to avoid, but it's getting messy. Might be a while until I get around to fixing this.

@bmatcuk
Copy link
Owner

bmatcuk commented Dec 1, 2019

@karlism: Good news! I spent the day on it and I just cut v1.2.0 which I think will work for you. I had to make a lot of changes to the code, so it's possible that a bug or two crept in, but it's passing all of my tests plus a few I added specifically for this feature. Let me know if you run into any problems!

@karlism
Copy link
Author

karlism commented Dec 2, 2019

@bmatcuk, thanks a lot for working on it!
I will test v1.2.0 in both in my example code and Loki and will let you know about results.

@karlism
Copy link
Author

karlism commented Dec 2, 2019

@bmatcuk, I did some test and unfortunately I cannot get it to work as expected:

$ cat example.go     
package main

import (
    "fmt"
    "github.com/bmatcuk/doublestar"
)

func main() {
    patterns := [11]string{
        "/tmp/test/*",
        "/tmp/test/{example1}",
        "/tmp/test/{example*}",
        "/tmp/test/{example1,example2}",
        "{/tmp/test/example*}",       // not working
        "{\\/tmp\\/test\\/example1}", // not working
        "{/tmp/test/example1}",	      // new - not working
        "{/tmp/test/example1,/tmp/test/example2}",	      // new - not working
        // "/tmp/test/example1,/tmp/test/example2",	      // ERROR: ./example.go:20:9: array index 10 out of bounds [0:10]
        "{/tmp/test/*}", 	      // new - not working
        "/tmp/{test}/*", 	      // new - working
        "{/tmp/test}/*"} 	      // new - not working
    for _, pattern := range patterns {
        fmt.Println("pattern:", pattern)
        matches, err := doublestar.Glob(pattern)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println(" ", matches, "\n")
        }
    }
}

$ go run example.go  
pattern: /tmp/test/*
  [/tmp/test/example1 /tmp/test/example2] 

pattern: /tmp/test/{example1}
  [/tmp/test/example1] 

pattern: /tmp/test/{example*}
  [/tmp/test/example1 /tmp/test/example2] 

pattern: /tmp/test/{example1,example2}
  [/tmp/test/example1 /tmp/test/example2] 

pattern: {/tmp/test/example*}
  [] 

pattern: {\/tmp\/test\/example1}
  [] 

pattern: {/tmp/test/example1}
  [] 

pattern: {/tmp/test/example1,/tmp/test/example2}
  [] 

pattern: {/tmp/test/*}
  [] 

pattern: /tmp/{test}/*
  [/tmp/test/example1 /tmp/test/example2]

pattern: {/tmp/test}/*
  [] 

I made sure that doublestar is updated to latest version prior to running this code.
I would expect following patterns to work:

  • {/tmp/test/example*}
  • {/tmp/test/*}
  • {/tmp/test/example1}
  • {/tmp/test/example1,/tmp/test/example2}

And /tmp/test/example1,/tmp/test/example2 to produce proper syntax error message.

@bmatcuk
Copy link
Owner

bmatcuk commented Dec 2, 2019

Nuts, thanks for the thorough testing! I'll see if I can get some fixes ASAP

@bmatcuk
Copy link
Owner

bmatcuk commented Dec 2, 2019

@karlism v1.2.1 should fix these issues. I was unable to reproduce an error with the pattern /tmp/test/example1,/tmp/test/example2 - it simply returns no results. Your sample program above, however, seems to work for all other test cases.

@karlism
Copy link
Author

karlism commented Dec 3, 2019

@bmatcuk, thank you! I've just tested everything and it works properly now.
As for the array index 10 out of bounds [0:10] error, it was my mistake, as I had forgot to adjust length of patterns array after adding another element to it, so go was complaining about length mismatch in my code. I will also test v1.2.1 on Loki, but I do not expect any surprises there.
Thanks again for prompt fix!

@karlism karlism closed this as completed Dec 3, 2019
@bmatcuk
Copy link
Owner

bmatcuk commented Dec 3, 2019

You're welcome! Let me know if you run into any other trouble with doublestar =)

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