Skip to content

Commit 8d206b7

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

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
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: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ 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"
13+
"github.com/bmatcuk/doublestar"
1414
"github.com/mattn/go-zglob"
1515
log "github.com/sirupsen/logrus"
1616
)
@@ -75,7 +75,7 @@ type Plugin struct {
7575
PathStyle bool
7676
// Dry run without uploading/
7777
DryRun bool
78-
// Regex for which files to remove from target
78+
// Glob for which files to remove from target
7979
TargetRemove string
8080
}
8181

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

120120
if len(p.TargetRemove) != 0 {
121-
reg, regexperr := regexp.Compile(p.TargetRemove)
122-
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-
}
130121

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

135-
log.Info("Listing files in bucket") // @ToDo: Log.Debug
126+
log.Info("Listing files in bucket")
136127
list_input := &s3.ListObjectsInput{
137128
Bucket: &p.Bucket,
138129
}
@@ -148,7 +139,18 @@ func (p *Plugin) Exec() error {
148139
var to_remove []string
149140
for _, object := range s3_objects.Contents {
150141
filename := object.Key
151-
if reg.MatchString(*filename) {
142+
143+
globmatch, globerr := doublestar.PathMatch(p.TargetRemove, *filename)
144+
145+
if globerr != nil {
146+
log.WithFields(log.Fields{
147+
"error": globerr,
148+
"glob": p.TargetRemove,
149+
}).Error("Error with provided glob")
150+
return globerr
151+
}
152+
153+
if globmatch {
152154
to_remove = append(to_remove, *filename)
153155
}
154156
}

0 commit comments

Comments
 (0)