mirror of
https://github.com/containers/podman.git
synced 2025-06-30 07:26:39 +08:00
Merge pull request #12364 from flouthoc/fix-filter-pattern
filter: use `filepath.Match` to maintain consistency with other pattern matching in podman
This commit is contained in:
@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -95,24 +95,13 @@ func PrepareFilters(r *http.Request) (*map[string][]string, error) {
|
|||||||
return &filterMap, nil
|
return &filterMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func wildCardToRegexp(pattern string) string {
|
|
||||||
var result strings.Builder
|
|
||||||
for i, literal := range strings.Split(pattern, "*") {
|
|
||||||
// Replace * with .*
|
|
||||||
if i > 0 {
|
|
||||||
result.WriteString(".*")
|
|
||||||
}
|
|
||||||
// Quote any regular expression meta characters in the
|
|
||||||
// literal text.
|
|
||||||
result.WriteString(regexp.QuoteMeta(literal))
|
|
||||||
}
|
|
||||||
return result.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func matchPattern(pattern string, value string) bool {
|
func matchPattern(pattern string, value string) bool {
|
||||||
if strings.Contains(pattern, "*") {
|
if strings.Contains(pattern, "*") {
|
||||||
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
|
filter := fmt.Sprintf("*%s*", pattern)
|
||||||
return result
|
filter = strings.ReplaceAll(filter, string(filepath.Separator), "|")
|
||||||
|
newName := strings.ReplaceAll(value, string(filepath.Separator), "|")
|
||||||
|
match, _ := filepath.Match(filter, newName)
|
||||||
|
return match
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user