Skip to content

Commit 06ac8b1

Browse files
authored
feat: support recursive pattern matching in file fetching (#405)
* feat: support recursive pattern matching in file fetching - Introduced the github.com/bmatcuk/doublestar/v4 dependency to support recursive pattern matching in file fetching. - Updated fetch_test.go to include tests for new pattern matching capabilities, including subdirectory and wildcard patterns. - Adjusted file paths in tests to reflect new structure. Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com> * refactor: updated the dragonfly function to utilize doublestar.Match - Updated the fetchByDragonfly function to utilize doublestar.Match, allowing for both simple and recursive pattern matching in file fetching. Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com> * refactor: replace doublestar.Match with doublestar.PathMatch for improved pattern matching - Updated the fetchByDragonfly and Fetch functions to use doublestar.PathMatch, enhancing pattern matching capabilities to support system-native path separators and recursive patterns. Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com> --------- Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com>
1 parent 45e0ec3 commit 06ac8b1

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ require (
6262
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 // indirect
6363
github.com/aws/smithy-go v1.24.0 // indirect
6464
github.com/beorn7/perks v1.0.1 // indirect
65+
github.com/bmatcuk/doublestar/v4 v4.10.0 // indirect
6566
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
6667
github.com/cespare/xxhash/v2 v2.3.0 // indirect
6768
github.com/clipperhouse/stringish v0.1.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
7777
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
7878
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
7979
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
80+
github.com/bmatcuk/doublestar/v4 v4.10.0 h1:zU9WiOla1YA122oLM6i4EXvGW62DvKZVxIe6TYWexEs=
81+
github.com/bmatcuk/doublestar/v4 v4.10.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
8082
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
8183
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
8284
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

pkg/backend/fetch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"path/filepath"
2423

24+
"github.com/bmatcuk/doublestar/v4"
2525
legacymodelspec "github.com/dragonflyoss/model-spec/specs-go/v1"
2626
modelspec "github.com/modelpack/model-spec/specs-go/v1"
2727
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -78,7 +78,10 @@ func (b *backend) Fetch(ctx context.Context, target string, cfg *config.Fetch) e
7878
if path == "" {
7979
path = anno[legacymodelspec.AnnotationFilepath]
8080
}
81-
matched, err := filepath.Match(pattern, path)
81+
// Use doublestar.PathMatch for pattern matching to support ** recursive matching
82+
// PathMatch uses the system's native path separator (like filepath.Match) while
83+
// also supporting recursive patterns like **/*.json
84+
matched, err := doublestar.PathMatch(pattern, path)
8285
if err != nil {
8386
return fmt.Errorf("failed to match pattern: %w", err)
8487
}

pkg/backend/fetch_by_d7y.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
common "d7y.io/api/v2/pkg/apis/common/v2"
2929
dfdaemon "d7y.io/api/v2/pkg/apis/dfdaemon/v2"
3030
"github.com/avast/retry-go/v4"
31+
"github.com/bmatcuk/doublestar/v4"
3132
legacymodelspec "github.com/dragonflyoss/model-spec/specs-go/v1"
3233
modelspec "github.com/modelpack/model-spec/specs-go/v1"
3334
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -81,7 +82,10 @@ func (b *backend) fetchByDragonfly(ctx context.Context, target string, cfg *conf
8182
if path == "" {
8283
path = anno[legacymodelspec.AnnotationFilepath]
8384
}
84-
matched, err := filepath.Match(pattern, path)
85+
// Use doublestar.PathMatch for pattern matching to support ** recursive matching
86+
// PathMatch uses the system's native path separator (like filepath.Match) while
87+
// also supporting recursive patterns like **/*.json
88+
matched, err := doublestar.PathMatch(pattern, path)
8589
if err != nil {
8690
return fmt.Errorf("failed to match pattern: %w", err)
8791
}

pkg/backend/fetch_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestFetch(t *testing.T) {
7373
Digest: file2Digest,
7474
Size: int64(len(file2Content)),
7575
Annotations: map[string]string{
76-
modelspec.AnnotationFilepath: "file2.txt",
76+
modelspec.AnnotationFilepath: "subdir/file2.txt",
7777
},
7878
},
7979
},
@@ -118,11 +118,33 @@ func TestFetch(t *testing.T) {
118118
expectError: false,
119119
},
120120
{
121-
name: "fetch with pattern matching both files",
121+
name: "fetch with pattern matching subdirectory file",
122122
target: url + "/test/model:latest",
123123
cfg: &config.Fetch{
124124
Output: tempDir,
125-
Patterns: []string{"file*.txt"},
125+
Patterns: []string{"subdir/*.txt"},
126+
PlainHTTP: true,
127+
Concurrency: 2,
128+
},
129+
expectError: false,
130+
},
131+
{
132+
name: "fetch with recursive pattern matching all txt files",
133+
target: url + "/test/model:latest",
134+
cfg: &config.Fetch{
135+
Output: tempDir,
136+
Patterns: []string{"**/*.txt"},
137+
PlainHTTP: true,
138+
Concurrency: 2,
139+
},
140+
expectError: false,
141+
},
142+
{
143+
name: "fetch with wildcard pattern (old behavior)",
144+
target: url + "/test/model:latest",
145+
cfg: &config.Fetch{
146+
Output: tempDir,
147+
Patterns: []string{"*.txt"},
126148
PlainHTTP: true,
127149
Concurrency: 2,
128150
},

0 commit comments

Comments
 (0)