@@ -124,32 +124,21 @@ func GetFileURL(filename string, bucket string) string {
124
124
return "https://s3.amazonaws.com" + path .Join ("/" , filename )
125
125
}
126
126
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 ) {
130
129
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
148
140
}
149
-
150
- fileURL = GetFileURL (filename , bucket )
151
- fmt .Printf ("%-10s %s\n " , action , fileURL )
152
- return fileURL , nil
141
+ return nil
153
142
}
154
143
155
144
// VersionAndUploadFiles will verion files and upload them to s3 and return
@@ -158,6 +147,7 @@ func VersionAndUploadFiles(
158
147
bucket string ,
159
148
directory string ,
160
149
filenames []string ,
150
+ dryRun bool ,
161
151
) (map [string ]string , error ) {
162
152
fileVersions := map [string ]string {}
163
153
@@ -178,11 +168,22 @@ func VersionAndUploadFiles(
178
168
uploadFilename = GetVersionedFilename (filename , checksum )
179
169
}
180
170
bucketFilename := path .Join (directory , uploadFilename )
171
+ fileURL := GetFileURL (filename , bucket )
181
172
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
+ }
185
179
}
180
+
181
+ if shouldUpload {
182
+ fmt .Printf ("%-10s %s\n " , "Uploaded" , fileURL )
183
+ } else {
184
+ fmt .Printf ("%-10s %s\n " , "Skipped" , fileURL )
185
+ }
186
+
186
187
fileVersions [filename ] = fileURL
187
188
}
188
189
@@ -193,7 +194,8 @@ func main() {
193
194
s3Bucket := flag .String ("bucket" , defaultS3Bucket , "the s3 bucket to upload to" )
194
195
directory := flag .String ("dir" , "" , "required, the directory to upload files to in the bucket" )
195
196
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" )
197
199
printVersion := flag .Bool ("v" , false , "print the current buffer-static-upload version" )
198
200
flag .Parse ()
199
201
@@ -214,7 +216,7 @@ func main() {
214
216
fmt .Printf ("Found %d files to upload and version:\n " , len (files ))
215
217
216
218
SetupS3Uploader ()
217
- fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files )
219
+ fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun )
218
220
if err != nil {
219
221
fatal ("failed to upload files %s" , err )
220
222
}
@@ -224,15 +226,22 @@ func main() {
224
226
fatal ("failed to generate versions json file %s" , err )
225
227
}
226
228
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
+ }
230
234
}
231
235
232
236
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
+ }
238
247
}
0 commit comments