@@ -124,32 +124,21 @@ func GetFileURL(filename string, bucket string) string {
124124 return "https://s3.amazonaws.com" + path .Join ("/" , filename )
125125}
126126
127- // UploadFile ensures a given file is uploaded to the s3 bucket and returns
128- // the filename
129- func UploadFile (file * os.File , filename string , bucket string ) (fileURL string , err error ) {
127+ // UploadFile uploads a given file to the s3 bucket
128+ func UploadFile (file * os.File , filename string , bucket string ) (err error ) {
130129 mimeType := GetFileMimeType (filename )
131-
132- var action string
133- if ! HasPreviousUpload (svc , bucket , filename ) {
134- _ , err := uploader .Upload (& s3manager.UploadInput {
135- Bucket : aws .String (bucket ),
136- Key : aws .String (filename ),
137- ContentType : aws .String (mimeType ),
138- CacheControl : aws .String ("public, max-age=31520626" ),
139- Expires : aws .Time (time .Now ().AddDate (10 , 0 , 0 )),
140- Body : file ,
141- })
142- if err != nil {
143- return fileURL , err
144- }
145- action = "Uploaded"
146- } else {
147- action = "Skipped"
130+ _ , err = uploader .Upload (& s3manager.UploadInput {
131+ Bucket : aws .String (bucket ),
132+ Key : aws .String (filename ),
133+ ContentType : aws .String (mimeType ),
134+ CacheControl : aws .String ("public, max-age=31520626" ),
135+ Expires : aws .Time (time .Now ().AddDate (10 , 0 , 0 )),
136+ Body : file ,
137+ })
138+ if err != nil {
139+ return err
148140 }
149-
150- fileURL = GetFileURL (filename , bucket )
151- fmt .Printf ("%-10s %s\n " , action , fileURL )
152- return fileURL , nil
141+ return nil
153142}
154143
155144// VersionAndUploadFiles will verion files and upload them to s3 and return
@@ -158,6 +147,7 @@ func VersionAndUploadFiles(
158147 bucket string ,
159148 directory string ,
160149 filenames []string ,
150+ dryRun bool ,
161151) (map [string ]string , error ) {
162152 fileVersions := map [string ]string {}
163153
@@ -178,11 +168,22 @@ func VersionAndUploadFiles(
178168 uploadFilename = GetVersionedFilename (filename , checksum )
179169 }
180170 bucketFilename := path .Join (directory , uploadFilename )
171+ fileURL := GetFileURL (filename , bucket )
181172
182- fileURL , err := UploadFile (file , bucketFilename , bucket )
183- if err != nil {
184- return fileVersions , err
173+ shouldUpload := ! HasPreviousUpload (svc , bucket , bucketFilename )
174+ if shouldUpload && ! dryRun {
175+ err := UploadFile (file , bucketFilename , bucket )
176+ if err != nil {
177+ return fileVersions , err
178+ }
185179 }
180+
181+ if shouldUpload {
182+ fmt .Printf ("%-10s %s\n " , "Uploaded" , fileURL )
183+ } else {
184+ fmt .Printf ("%-10s %s\n " , "Skipped" , fileURL )
185+ }
186+
186187 fileVersions [filename ] = fileURL
187188 }
188189
@@ -193,7 +194,8 @@ func main() {
193194 s3Bucket := flag .String ("bucket" , defaultS3Bucket , "the s3 bucket to upload to" )
194195 directory := flag .String ("dir" , "" , "required, the directory to upload files to in the bucket" )
195196 filesArg := flag .String ("files" , "" , "the path to the files you'd like to upload, ex. \" public/**/.*js,public/style.css\" " )
196- outputFilename := flag .String ("o" , "staticAssets.json" , "the json file you'd like your generate" )
197+ outputFilename := flag .String ("o" , "staticAssets.json" , "the filename for the versions manifest" )
198+ dryRun := flag .Bool ("dry-run" , false , "print the output only, skip file uploads and manifest creation" )
197199 printVersion := flag .Bool ("v" , false , "print the current buffer-static-upload version" )
198200 flag .Parse ()
199201
@@ -214,7 +216,7 @@ func main() {
214216 fmt .Printf ("Found %d files to upload and version:\n " , len (files ))
215217
216218 SetupS3Uploader ()
217- fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files )
219+ fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun )
218220 if err != nil {
219221 fatal ("failed to upload files %s" , err )
220222 }
@@ -224,15 +226,22 @@ func main() {
224226 fatal ("failed to generate versions json file %s" , err )
225227 }
226228
227- err = ioutil .WriteFile (* outputFilename , output , 0644 )
228- if err != nil {
229- fatal ("failed to write versions json file %s" , err )
229+ if ! * dryRun {
230+ err = ioutil .WriteFile (* outputFilename , output , 0644 )
231+ if err != nil {
232+ fatal ("failed to write versions json file %s" , err )
233+ }
230234 }
231235
232236 elapsed := time .Since (start )
233- fmt .Printf (
234- "\n Successfully uploaded static assets and generated %s in %s\n " ,
235- * outputFilename ,
236- elapsed ,
237- )
237+ if * dryRun {
238+ fmt .Printf ("\n Completed dry run in %s\n " , elapsed )
239+ } else {
240+
241+ fmt .Printf (
242+ "\n Successfully uploaded static assets and generated %s in %s\n " ,
243+ * outputFilename ,
244+ elapsed ,
245+ )
246+ }
238247}
0 commit comments