|
4 | 4 | "mime" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "regexp" |
7 | 8 | "strings" |
8 | 9 |
|
9 | 10 | "github.com/aws/aws-sdk-go/aws" |
@@ -74,6 +75,8 @@ type Plugin struct { |
74 | 75 | PathStyle bool |
75 | 76 | // Dry run without uploading/ |
76 | 77 | DryRun bool |
| 78 | + // Regex for which files to remove from target |
| 79 | + TargetRemove string |
77 | 80 | } |
78 | 81 |
|
79 | 82 | // Exec runs the plugin |
@@ -114,6 +117,81 @@ func (p *Plugin) Exec() error { |
114 | 117 | return err |
115 | 118 | } |
116 | 119 |
|
| 120 | + 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 | + } |
| 130 | + |
| 131 | + log.WithFields(log.Fields{ |
| 132 | + "regexp": p.TargetRemove, |
| 133 | + }).Info("Deleting files according to regexp") |
| 134 | + |
| 135 | + log.Info("Listing files in bucket") // @ToDo: Log.Debug |
| 136 | + list_input := &s3.ListObjectsInput{ |
| 137 | + Bucket: &p.Bucket, |
| 138 | + } |
| 139 | + |
| 140 | + s3_objects, list_err := client.ListObjects(list_input) |
| 141 | + if list_err != nil { |
| 142 | + log.WithFields(log.Fields{ |
| 143 | + "error": list_err, |
| 144 | + }).Error("Error listing objects from bucket") |
| 145 | + return list_err |
| 146 | + } |
| 147 | + |
| 148 | + var to_remove []string |
| 149 | + for _, object := range s3_objects.Contents { |
| 150 | + filename := object.Key |
| 151 | + if reg.MatchString(*filename) { |
| 152 | + to_remove = append(to_remove, *filename) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if len(to_remove) > 0 { |
| 157 | + log.WithFields(log.Fields{ |
| 158 | + "num_files": len(to_remove), |
| 159 | + }).Info("Deleting files from bucket") |
| 160 | + |
| 161 | + var remove_identifiers []*s3.ObjectIdentifier |
| 162 | + for _, key := range to_remove { |
| 163 | + id := s3.ObjectIdentifier{ |
| 164 | + Key: aws.String(key), |
| 165 | + } |
| 166 | + remove_identifiers = append(remove_identifiers, &id) |
| 167 | + } |
| 168 | + |
| 169 | + delete_input := &s3.DeleteObjectsInput{ |
| 170 | + Bucket: &p.Bucket, |
| 171 | + Delete: &s3.Delete{ |
| 172 | + Objects: remove_identifiers, |
| 173 | + Quiet: aws.Bool(false), |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + // when executing a dry-run we skip this step because we don't actually |
| 178 | + // want to remove files from S3. |
| 179 | + if !p.DryRun { |
| 180 | + log.WithFields(log.Fields{ |
| 181 | + "num_files": len(remove_identifiers), |
| 182 | + }).Info("Attempting to delete files") |
| 183 | + _, delete_err := client.DeleteObjects(delete_input) |
| 184 | + |
| 185 | + if delete_err != nil { |
| 186 | + log.WithFields(log.Fields{ |
| 187 | + "error": delete_err, |
| 188 | + }).Error("Error deleting objects from S3") |
| 189 | + return delete_err |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
117 | 195 | for _, match := range matches { |
118 | 196 |
|
119 | 197 | stat, err := os.Stat(match) |
|
0 commit comments