Skip to content

Commit 536287d

Browse files
committed
Support file matching for folders
This allows go-getter to not download everything in a folder Our usecase is that our S3 bucket contains large files and metadata and we want to only get metadata With this, we can set a filename regex on the extension bcfc71 I just cherry picked the changes that Julien did 4 years ago to make ensure that we don't break our fork of this repo but ensuring that we are up to date since there are vulnerabilities in the version we used. We will have to check if the changes are still required and maybe reopen the PR that was orignially opened by Julien (hashicorp#289)
1 parent 5a63fd9 commit 536287d

4 files changed

+41
-0
lines changed

client.go

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ type Client struct {
8383
// Disable symlinks
8484
DisableSymlinks bool
8585

86+
// FileMatches allows to filter files that should be fetched from a source directory
87+
// By default, all files are matched
88+
FileMatches FileMatcher
89+
8690
Options []ClientOption
8791
}
8892

client_option.go

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func (c *Client) Configure(opts ...ClientOption) error {
1717
c.Ctx = context.Background()
1818
}
1919

20+
c.FileMatches = func(string, string) bool {
21+
return true
22+
}
23+
24+
2025
// Store the options used to configure this client.
2126
c.Options = opts
2227

client_option_matching_files.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package getter
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// FileMatcher allows to filter files based on their source and name
9+
type FileMatcher = func(source, fullFilename string) bool
10+
11+
// WithFileMatcher adds a FileMatcher to the client
12+
// Any kind of function that returns a boolean from file properties can be used
13+
func WithFileMatcher(fm FileMatcher) func(*Client) error {
14+
return func(c *Client) error {
15+
c.FileMatches = fm
16+
return nil
17+
}
18+
}
19+
20+
// WithFileMatcher adds a regex FileMatcher to the client
21+
func WithRegexFileMatcher(regex string) func(*Client) error {
22+
return func(c *Client) error {
23+
expression, err := regexp.Compile(regex)
24+
c.FileMatches = func(source, fullFilename string) bool {
25+
return expression.MatchString(fullFilename) || expression.MatchString((strings.TrimPrefix(fullFilename, source)))
26+
}
27+
return err
28+
}
29+
}

get_s3.go

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ func (g *S3Getter) Get(dst string, u *url.URL) error {
138138
for _, object := range resp.Contents {
139139
lastMarker = aws.StringValue(object.Key)
140140
objPath := aws.StringValue(object.Key)
141+
if !g.client.FileMatches(path, objPath) {
142+
continue
143+
}
141144

142145
// If the key ends with a backslash assume it is a directory and ignore
143146
if strings.HasSuffix(objPath, "/") {

0 commit comments

Comments
 (0)