-
Notifications
You must be signed in to change notification settings - Fork 67
Make it possible to supply a glob for matching files to remove from S3 #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
801ed2e
8d206b7
d3ede08
ea8d413
caada99
648ca2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ import ( | |||||||||
| "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" | ||||||||||
| ) | ||||||||||
|
|
@@ -74,6 +75,8 @@ type Plugin struct { | |||||||||
| PathStyle bool | ||||||||||
| // Dry run without uploading/ | ||||||||||
| DryRun bool | ||||||||||
| // Glob for which files to remove from target | ||||||||||
| TargetRemove string | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Exec runs the plugin | ||||||||||
|
|
@@ -114,6 +117,83 @@ func (p *Plugin) Exec() error { | |||||||||
| return err | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if len(p.TargetRemove) != 0 { | ||||||||||
|
|
||||||||||
| log.WithFields(log.Fields{ | ||||||||||
| "glob": p.TargetRemove, | ||||||||||
| }).Info("Deleting files according to glob") | ||||||||||
|
|
||||||||||
| log.Info("Listing files in bucket") | ||||||||||
| list_input := &s3.ListObjectsInput{ | ||||||||||
| Bucket: &p.Bucket, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| s3_objects, list_err := client.ListObjects(list_input) | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| if list_err != nil { | ||||||||||
| log.WithFields(log.Fields{ | ||||||||||
| "error": list_err, | ||||||||||
| }).Error("Error listing objects from bucket") | ||||||||||
| return list_err | ||||||||||
| } | ||||||||||
|
|
||||||||||
| var to_remove []string | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| for _, object := range s3_objects.Contents { | ||||||||||
| filename := object.Key | ||||||||||
|
|
||||||||||
| globmatch, globerr := doublestar.PathMatch(p.TargetRemove, *filename) | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| 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) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if len(to_remove) > 0 { | ||||||||||
| log.WithFields(log.Fields{ | ||||||||||
| "num_files": len(to_remove), | ||||||||||
| }).Info("Deleting files from bucket") | ||||||||||
|
|
||||||||||
| var remove_identifiers []*s3.ObjectIdentifier | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| for _, key := range to_remove { | ||||||||||
| id := s3.ObjectIdentifier{ | ||||||||||
| Key: aws.String(key), | ||||||||||
| } | ||||||||||
| remove_identifiers = append(remove_identifiers, &id) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| delete_input := &s3.DeleteObjectsInput{ | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| Bucket: &p.Bucket, | ||||||||||
| Delete: &s3.Delete{ | ||||||||||
| Objects: remove_identifiers, | ||||||||||
| Quiet: aws.Bool(false), | ||||||||||
| }, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // when executing a dry-run we skip this step because we don't actually | ||||||||||
| // want to remove files from S3. | ||||||||||
| if !p.DryRun { | ||||||||||
| log.WithFields(log.Fields{ | ||||||||||
| "num_files": len(remove_identifiers), | ||||||||||
| }).Info("Attempting to delete files") | ||||||||||
| _, delete_err := client.DeleteObjects(delete_input) | ||||||||||
tboerger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| if delete_err != nil { | ||||||||||
| log.WithFields(log.Fields{ | ||||||||||
|
||||||||||
| if err != nil { | |
| log.WithFields(log.Fields{ | |
| "error": err, | |
| }).Error("Could not match files") |
I'm fine with removing my additions which do this, though. The only concern is that maybe the error messages from S3 are more obscure than the ones provided by the logs.
Uh oh!
There was an error while loading. Please reload this page.