Skip to content

Commit 9408d3b

Browse files
committed
feat: Use globbing instead of regexp for path matching files to remove.
1 parent 801ed2e commit 9408d3b

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/drone-plugins/drone-s3
22

33
require (
44
github.com/aws/aws-sdk-go v1.16.17
5+
github.com/bmatcuk/doublestar v1.1.1
56
github.com/joho/godotenv v1.3.0
67
github.com/mattn/go-zglob v0.0.1
78
github.com/sirupsen/logrus v1.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/aws/aws-sdk-go v1.16.17 h1:hHRKZhoB4qEY17aGNp71UxQFyYpx6WZXGMUzx9y/A4w=
22
github.com/aws/aws-sdk-go v1.16.17/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
3+
github.com/bmatcuk/doublestar v1.1.1 h1:YroD6BJCZBYx06yYFEWvUuKVWQn3vLLQAVmDmvTSaiQ=
4+
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
35
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
46
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func main() {
100100
},
101101
cli.StringFlag{
102102
Name: "target-remove",
103-
Usage: "regex for which files to remove from target",
103+
Usage: "glob for which files to remove from target",
104104
EnvVar: "PLUGIN_TARGET_REMOVE",
105105
},
106106
}

plugin.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"mime"
55
"os"
66
"path/filepath"
7-
"regexp"
87
"strings"
98

109
"github.com/aws/aws-sdk-go/aws"
1110
"github.com/aws/aws-sdk-go/aws/credentials"
1211
"github.com/aws/aws-sdk-go/aws/session"
1312
"github.com/aws/aws-sdk-go/service/s3"
1413
"github.com/mattn/go-zglob"
14+
"github.com/bmatcuk/doublestar"
1515
log "github.com/sirupsen/logrus"
1616
)
1717

@@ -118,21 +118,13 @@ func (p *Plugin) Exec() error {
118118
}
119119

120120
if len(p.TargetRemove) != 0 {
121-
reg, regexperr := regexp.Compile(p.TargetRemove)
122121

123-
if regexperr != nil {
124-
log.WithFields(log.Fields{
125-
"error": regexperr,
126-
"regexp": p.TargetRemove,
127-
}).Error("Regular expression failed to compile")
128-
return regexperr
129-
}
130122

131123
log.WithFields(log.Fields{
132-
"regexp": p.TargetRemove,
133-
}).Info("Deleting files according to regexp")
124+
"glob": p.TargetRemove,
125+
}).Info("Deleting files according to glob")
134126

135-
log.Info("Listing files in bucket") // @ToDo: Log.Debug
127+
log.Info("Listing files in bucket")
136128
list_input := &s3.ListObjectsInput{
137129
Bucket: &p.Bucket,
138130
}
@@ -148,7 +140,18 @@ func (p *Plugin) Exec() error {
148140
var to_remove []string
149141
for _, object := range s3_objects.Contents {
150142
filename := object.Key
151-
if reg.MatchString(*filename) {
143+
144+
globmatch, globerr := doublestar.PathMatch(p.TargetRemove, *filename)
145+
146+
if (globerr != nil) {
147+
log.WithFields(log.Fields{
148+
"error": globerr,
149+
"glob": p.TargetRemove,
150+
}).Error("Error with provided glob")
151+
return globerr
152+
}
153+
154+
if (globmatch) {
152155
to_remove = append(to_remove, *filename)
153156
}
154157
}

0 commit comments

Comments
 (0)