diff --git a/glide.lock b/glide.lock index 3913356..fd1f2dd 100644 --- a/glide.lock +++ b/glide.lock @@ -1,6 +1,47 @@ -hash: 7a6c0cb5f1bdb57d6eeec927bb218fea1867a05a4007c113b09b15e8edf6cc89 -updated: 2017-05-24T14:46:15.136419047-07:00 +hash: e1da4bc94060e653a11f1a2db9e10096e968fa1c5bf09db88db8c9303961843d +updated: 2019-01-22T11:49:24.767164+01:00 imports: - name: github.com/aws/aws-sdk-go - version: e8177d9867544fa4677f40ceb470e60b6f3573ae + version: 96358b8282b1a3aa66836d2f2fe66216cc419668 + subpackages: + - aws + - aws/awserr + - aws/awsutil + - aws/client + - aws/client/metadata + - aws/corehandlers + - aws/credentials + - aws/credentials/ec2rolecreds + - aws/credentials/endpointcreds + - aws/credentials/stscreds + - aws/defaults + - aws/ec2metadata + - aws/endpoints + - aws/request + - aws/session + - aws/signer/v4 + - internal/shareddefaults + - private/protocol + - private/protocol/query + - private/protocol/query/queryutil + - private/protocol/rest + - private/protocol/restxml + - private/protocol/xml/xmlutil + - service/s3 + - service/s3/s3iface + - service/s3/s3manager + - service/sts +- name: github.com/go-ini/ini + version: 6ed8d5f64cd79a498d1f3fab5880cc376ce41bbe +- name: github.com/jmespath/go-jmespath + version: c2b33e8439af944379acbdd9c3a5fe0bc44bd8a5 +- name: github.com/spf13/afero + version: a5d6946387efe7d64d09dcba68cdd523dc1273a3 + subpackages: + - mem +- name: golang.org/x/text + version: f21a4dfb5e38f5895301dc265a8def02365cc3d0 + subpackages: + - transform + - unicode/norm testImports: [] diff --git a/glide.yaml b/glide.yaml index fc91293..efaab0a 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,3 +2,9 @@ package: . import: - package: github.com/aws/aws-sdk-go version: ~1.8.29 +- package: github.com/spf13/afero + version: ^1.2.0 +- package: golang.org/x/text + version: ^0.3.0 + subpackages: + - transform diff --git a/main.go b/main.go index a71fd7e..b829897 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func fatal(format string, a ...interface{}) { } // GetFileMd5 returns a checksum for a given file -func GetFileMd5(file *os.File) (string, error) { +func GetFileMd5(file io.Reader) (string, error) { var fileHash string hash := md5.New() if _, err := io.Copy(hash, file); err != nil { @@ -143,6 +143,23 @@ func UploadFile(file *os.File, filename string, bucket string) (err error) { return nil } +func ShouldVersionFile(filename string, skipVersioning bool) (bool) { + ext := filepath.Ext(filename) + return !skipVersioning && (ext == ".js" || ext == ".css") +} + +func GetUploadFilename(file io.Reader, filename string, skipVersioning bool) (string, error) { + uploadFilename := filename + if ShouldVersionFile(filename, skipVersioning) { + checksum, errMd5 := GetFileMd5(file) + if errMd5 != nil { + return "", errMd5 + } + uploadFilename = GetVersionedFilename(filename, checksum) + } + return uploadFilename, nil +} + // VersionAndUploadFiles will verion files and upload them to s3 and return // a map of filenames and their version hashes func VersionAndUploadFiles( @@ -150,6 +167,7 @@ func VersionAndUploadFiles( directory string, filenames []string, dryRun bool, + skipVersioning bool, ) (map[string]string, error) { fileVersions := map[string]string{} @@ -162,14 +180,9 @@ func VersionAndUploadFiles( } defer file.Close() - ext := filepath.Ext(filename) - uploadFilename := filename - if ext == ".js" || ext == ".css" { - checksum, errMd5 := GetFileMd5(file) - if errMd5 != nil { - return fileVersions, errMd5 - } - uploadFilename = GetVersionedFilename(filename, checksum) + uploadFilename, err := GetUploadFilename(file, filename, skipVersioning) + if err != nil { + return fileVersions, err } bucketFilename := path.Join(directory, uploadFilename) fileURL := GetFileURL(bucket, bucketFilename) @@ -220,6 +233,7 @@ func main() { format := flag.String("format", "json", "format of the output [json,csv]") dryRun := flag.Bool("dry-run", false, "print the output only, skip file uploads and manifest creation") printVersion := flag.Bool("v", false, "print the current buffer-static-upload version") + skipVersioning := flag.Bool("skip-versioning", false, "skip versioning uploaded files") flag.Parse() if *printVersion { @@ -239,7 +253,7 @@ func main() { fmt.Printf("Found %d files to upload and version:\n", len(files)) SetupS3Uploader() - fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun) + fileVersions, err := VersionAndUploadFiles(*s3Bucket, *directory, files, *dryRun, *skipVersioning) if err != nil { fatal("failed to upload files %s", err) } diff --git a/main_test.go b/main_test.go index 35e13bd..9dbca57 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,10 @@ package main import ( + "os" + "fmt" "testing" + "github.com/spf13/afero" ) func TestGetFileURL(t *testing.T) { @@ -21,3 +24,49 @@ func TestGetFileURL(t *testing.T) { } } } + + +func TestShouldVersionFile(t *testing.T) { + tests := []struct { + filename string + skipVersioning bool + expected bool + } { + { "bundle.js", false, true }, + { "bundle.js", true, false }, + { "assets.css", false, true }, + { "assets.css", true, false }, + { "another.file", false, false }, + } + + for _, test := range tests { + actual := ShouldVersionFile(test.filename, test.skipVersioning) + if actual != test.expected { + t.Errorf("ShouldVersionFile result was incorrect, got %t, expected %t for filename %s", actual, test.expected, test.filename) + } + } +} + +func TestGetUploadFilename(t *testing.T) { + var AppFs = afero.NewMemMapFs() + filename := "bundle.js" + file, _ := AppFs.OpenFile(filename, os.O_CREATE, 0600) + file.WriteString("some JS content") + tests := []struct { + file afero.File + filename string + skipVersioning bool + expected string + } { + { file, filename, false, "bundle.d41d8cd98f00b204e9800998ecf8427e.js" }, + { file, filename, true, "bundle.js" }, + } + + for _, test := range tests { + fmt.Print(file.Name()) + actual, _ := GetUploadFilename(test.file, test.filename, test.skipVersioning) + if actual != test.expected { + t.Errorf("GetUploadFilename result was incorrect, got %s, expected %s", actual, test.expected) + } + } +} \ No newline at end of file