Skip to content

Commit

Permalink
feat: Use globbing instead of regexp for path matching files to remove.
Browse files Browse the repository at this point in the history
  • Loading branch information
sklirg committed Feb 27, 2019
1 parent 801ed2e commit 8d206b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/drone-plugins/drone-s3

require (
github.com/aws/aws-sdk-go v1.16.17
github.com/bmatcuk/doublestar v1.1.1
github.com/joho/godotenv v1.3.0
github.com/mattn/go-zglob v0.0.1
github.com/sirupsen/logrus v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/aws/aws-sdk-go v1.16.17 h1:hHRKZhoB4qEY17aGNp71UxQFyYpx6WZXGMUzx9y/A4w=
github.com/aws/aws-sdk-go v1.16.17/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/bmatcuk/doublestar v1.1.1 h1:YroD6BJCZBYx06yYFEWvUuKVWQn3vLLQAVmDmvTSaiQ=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func main() {
},
cli.StringFlag{
Name: "target-remove",
Usage: "regex for which files to remove from target",
Usage: "glob for which files to remove from target",
EnvVar: "PLUGIN_TARGET_REMOVE",
},
}
Expand Down
32 changes: 17 additions & 15 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"mime"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/bmatcuk/doublestar"
"github.com/mattn/go-zglob"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -75,7 +75,7 @@ type Plugin struct {
PathStyle bool
// Dry run without uploading/
DryRun bool
// Regex for which files to remove from target
// Glob for which files to remove from target
TargetRemove string
}

Expand Down Expand Up @@ -118,21 +118,12 @@ func (p *Plugin) Exec() error {
}

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

if regexperr != nil {
log.WithFields(log.Fields{
"error": regexperr,
"regexp": p.TargetRemove,
}).Error("Regular expression failed to compile")
return regexperr
}

log.WithFields(log.Fields{
"regexp": p.TargetRemove,
}).Info("Deleting files according to regexp")
"glob": p.TargetRemove,
}).Info("Deleting files according to glob")

log.Info("Listing files in bucket") // @ToDo: Log.Debug
log.Info("Listing files in bucket")
list_input := &s3.ListObjectsInput{
Bucket: &p.Bucket,
}
Expand All @@ -148,7 +139,18 @@ func (p *Plugin) Exec() error {
var to_remove []string
for _, object := range s3_objects.Contents {
filename := object.Key
if reg.MatchString(*filename) {

globmatch, globerr := doublestar.PathMatch(p.TargetRemove, *filename)

if globerr != nil {
log.WithFields(log.Fields{
"error": globerr,
"glob": p.TargetRemove,
}).Error("Error with provided glob")
return globerr
}

if globmatch {
to_remove = append(to_remove, *filename)
}
}
Expand Down

0 comments on commit 8d206b7

Please sign in to comment.