Skip to content

Commit

Permalink
[Breaking Change] Skip now accepts FileInfo
Browse files Browse the repository at this point in the history
Close #40
  • Loading branch information
otiai10 committed Nov 9, 2022
1 parent 705b76e commit 8749e2c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Options struct {
OnDirExists func(src, dest string) DirExistsAction

// Skip can specify which files should be skipped
Skip func(src string) (bool, error)
Skip func(srcinfo os.FileInfo, src, dest string) (bool, error)

// PermissionControl can control permission of
// every entry.
Expand Down Expand Up @@ -80,7 +80,7 @@ type Options struct {
```go
// For example...
opt := Options{
Skip: func(src string) (bool, error) {
Skip: func(info os.FileInfo, src, dest string) (bool, error) {
return strings.HasSuffix(src, ".git"), nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestOptions_OnSymlink(t *testing.T) {
}

func TestOptions_Skip(t *testing.T) {
opt := Options{Skip: func(src string) (bool, error) {
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
switch {
case strings.HasSuffix(src, "_skip"):
return true, nil
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestOptions_Skip(t *testing.T) {

Because(t, "if Skip func returns error, Copy should be interrupted", func(t *testing.T) {
errInsideSkipFunc := errors.New("Something wrong inside Skip")
opt := Options{Skip: func(src string) (bool, error) {
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
return false, errInsideSkipFunc
}}
err := Copy("test/data/case06", "test/data.copy/case06.01", opt)
Expand Down
14 changes: 8 additions & 6 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) {
// Because this "copy" could be called recursively,
// "info" MUST be given here, NOT nil.
func copyNextOrSkip(src, dest string, info os.FileInfo, opt Options) error {
skip, err := opt.Skip(src)
if err != nil {
return err
}
if skip {
return nil
if opt.Skip != nil {
skip, err := opt.Skip(info, src, dest)
if err != nil {
return err
}
if skip {
return nil
}
}
return switchboard(src, dest, info, opt)
}
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ExampleOptions() {
"test/data/example",
"test/data.copy/example_with_options",
Options{
Skip: func(src string) (bool, error) {
Skip: func(info os.FileInfo, src, dest string) (bool, error) {
return strings.HasSuffix(src, ".git-like"), nil
},
OnSymlink: func(src string) SymlinkAction {
Expand Down
8 changes: 3 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Options struct {
OnDirExists func(src, dest string) DirExistsAction

// Skip can specify which files should be skipped
Skip func(src string) (bool, error)
Skip func(srcinfo os.FileInfo, src, dest string) (bool, error)

// Specials includes special files to be copied. default false.
Specials bool
Expand Down Expand Up @@ -94,10 +94,8 @@ func getDefaultOptions(src, dest string) Options {
OnSymlink: func(string) SymlinkAction {
return Shallow // Do shallow copy
},
OnDirExists: nil, // Default behavior is "Merge".
Skip: func(string) (bool, error) {
return false, nil // Don't skip
},
OnDirExists: nil, // Default behavior is "Merge".
Skip: nil, // Do not skip anything
AddPermission: 0, // Add nothing
PermissionControl: PerservePermission, // Just preserve permission
Sync: false, // Do not sync
Expand Down

0 comments on commit 8749e2c

Please sign in to comment.