diff --git a/README.md b/README.md index a1fc42f..c194104 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # drone-s3-sync [![Build Status](http://beta.drone.io/api/badges/drone-plugins/drone-s3-sync/status.svg)](http://beta.drone.io/drone-plugins/drone-s3-sync) -[![Go Doc](https://godoc.org/github.com/drone-plugins/drone-s3-sync?status.svg)](http://godoc.org/github.com/drone-plugins/drone-s3-sync) -[![Go Report](https://goreportcard.com/badge/github.com/drone-plugins/drone-s3-sync)](https://goreportcard.com/report/github.com/drone-plugins/drone-s3-sync) -[![Join the chat at https://gitter.im/drone/drone](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/drone/drone) Drone plugin to synchronize a directory with an Amazon S3 Bucket. For the usage information and a listing of the available options please take a look at diff --git a/aws.go b/aws.go index e4b9b4f..d0c36d2 100644 --- a/aws.go +++ b/aws.go @@ -108,8 +108,8 @@ func (a *AWS) Upload(local, remote string) error { Bucket: aws.String(p.Bucket), Key: aws.String(remote), }) - if err != nil { - if err.(awserr.Error).Code() != "404" { + if err != nil && err.(awserr.Error).Code() != "404" { + if err.(awserr.Error).Code() == "404" { return err } @@ -131,6 +131,11 @@ func (a *AWS) Upload(local, remote string) error { putObject.ContentEncoding = aws.String(contentEncoding) } + // skip upload during dry run + if a.plugin.DryRun { + return nil + } + _, err = a.client.PutObject(putObject) return err } @@ -246,6 +251,11 @@ func (a *AWS) Upload(local, remote string) error { copyObject.ContentEncoding = aws.String(contentEncoding) } + // skip update if dry run + if a.plugin.DryRun { + return nil + } + _, err = a.client.CopyObject(copyObject) return err } else { @@ -272,6 +282,11 @@ func (a *AWS) Upload(local, remote string) error { putObject.ContentEncoding = aws.String(contentEncoding) } + // skip upload if dry run + if a.plugin.DryRun { + return nil + } + _, err = a.client.PutObject(putObject) return err } @@ -280,6 +295,11 @@ func (a *AWS) Upload(local, remote string) error { func (a *AWS) Redirect(path, location string) error { p := a.plugin debug("Adding redirect from \"%s\" to \"%s\"", path, location) + + if a.plugin.DryRun { + return nil + } + _, err := a.client.PutObject(&s3.PutObjectInput{ Bucket: aws.String(p.Bucket), Key: aws.String(path), @@ -292,6 +312,11 @@ func (a *AWS) Redirect(path, location string) error { func (a *AWS) Delete(remote string) error { p := a.plugin debug("Removing remote file \"%s\"", remote) + + if a.plugin.DryRun { + return nil + } + _, err := a.client.DeleteObject(&s3.DeleteObjectInput{ Bucket: aws.String(p.Bucket), Key: aws.String(remote), diff --git a/main.go b/main.go index cf5129a..11e5b7a 100644 --- a/main.go +++ b/main.go @@ -97,6 +97,11 @@ func main() { Usage: "id of cloudfront distribution to invalidate", EnvVar: "PLUGIN_CLOUDFRONT_DISTRIBUTION", }, + cli.BoolFlag{ + Name: "dry-run", + Usage: "dry run disables api calls", + EnvVar: "DRY_RUN,PLUGIN_DRY_RUN", + }, cli.StringFlag{ Name: "env-file", Usage: "source env file", @@ -128,6 +133,7 @@ func run(c *cli.Context) error { Metadata: c.Generic("metadata").(*DeepStringMapFlag).Get(), Redirects: c.Generic("redirects").(*MapFlag).Get(), CloudFrontDistribution: c.String("cloudfront-distribution"), + DryRun: c.Bool("dry-run"), } return plugin.Exec() diff --git a/plugin.go b/plugin.go index f66204a..f814573 100644 --- a/plugin.go +++ b/plugin.go @@ -23,6 +23,7 @@ type Plugin struct { Metadata map[string]map[string]string Redirects map[string]string CloudFrontDistribution string + DryRun bool client AWS jobs []job }