Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds feature to exclude files from source #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

package s3sync

import "github.com/aws/aws-sdk-go/service/s3/s3manager"
import (
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gobwas/glob"
)

const (
// Default number of parallel file sync jobs.
Expand Down Expand Up @@ -52,6 +55,13 @@ func WithDryRun() Option {
}
}

// WithExcludePattern will exclude files from the sync.
func WithExcludePattern(pattern string) Option {
return func(m *Manager) {
m.exclude = append(m.exclude, glob.MustCompile(pattern))
}
}

// WithoutGuessMimeType disables guessing MIME type from contents.
func WithoutGuessMimeType() Option {
return func(m *Manager) {
Expand Down
23 changes: 20 additions & 3 deletions s3sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gabriel-vasile/mimetype"
"github.com/gobwas/glob"
)

// Manager manages the sync operation.
Expand All @@ -35,6 +36,7 @@ type Manager struct {
nJobs int
del bool
dryrun bool
exclude []glob.Glob
acl *string
guessMime bool
contentType *string
Expand Down Expand Up @@ -143,7 +145,7 @@ func (m *Manager) syncLocalToS3(chJob chan func(), sourcePath string, destPath *
wg := &sync.WaitGroup{}
errs := &multiErr{}
for source := range filterFilesForSync(
listLocalFiles(sourcePath), m.listS3Files(destPath), m.del,
listLocalFiles(sourcePath), m.listS3Files(destPath), m.del, m.exclude,
) {
wg.Add(1)
source := source
Expand Down Expand Up @@ -175,7 +177,7 @@ func (m *Manager) syncS3ToLocal(chJob chan func(), sourcePath *s3Path, destPath
wg := &sync.WaitGroup{}
errs := &multiErr{}
for source := range filterFilesForSync(
m.listS3Files(sourcePath), listLocalFiles(destPath), m.del,
m.listS3Files(sourcePath), listLocalFiles(destPath), m.del, m.exclude,
) {
wg.Add(1)
source := source
Expand Down Expand Up @@ -384,6 +386,7 @@ func (m *Manager) listS3FileWithToken(c chan *fileInfo, path *s3Path, token *str
// Skip directory like object
continue
}

name, err := filepath.Rel(path.bucketPrefix, *object.Key)
if err != nil {
sendErrorInfoToChannel(c, err)
Expand Down Expand Up @@ -476,7 +479,7 @@ func sendErrorInfoToChannel(c chan *fileInfo, err error) {

// filterFilesForSync filters the source files from the given destination files, and returns
// another channel which includes the files necessary to be synced.
func filterFilesForSync(sourceFileChan, destFileChan chan *fileInfo, del bool) chan *fileOp {
func filterFilesForSync(sourceFileChan, destFileChan chan *fileInfo, del bool, excludePatterns []glob.Glob) chan *fileOp {
c := make(chan *fileOp)

destFiles, err := fileInfoChanToMap(destFileChan)
Expand All @@ -488,6 +491,20 @@ func filterFilesForSync(sourceFileChan, destFileChan chan *fileInfo, del bool) c
return
}
for sourceInfo := range sourceFileChan {
exclude := func(name string, patterns []glob.Glob) bool {
for _, pattern := range patterns {
if pattern.Match(sourceInfo.name) {
return true
}
}

return false
}

if exclude(sourceInfo.name, excludePatterns) {
continue
}

destInfo, ok := destFiles[sourceInfo.name]
// source is necessary to sync if
// 1. The dest doesn't exist
Expand Down