Skip to content

Commit

Permalink
updated according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Apr 17, 2023
1 parent b348b50 commit 80eee8d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
13 changes: 5 additions & 8 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestOptions_CopyRateLimit(t *testing.T) {

func TestOptions_OnFileError(t *testing.T) {
opt := Options{
OnErr: nil,
OnError: nil,
}

// existing, process nromally
Expand All @@ -378,26 +378,23 @@ func TestOptions_OnFileError(t *testing.T) {
_, err = os.Stat("test/data.copy/case17/non-existing")
Expect(t, os.IsNotExist(err)).ToBe(true)

// existing, err not passed
var called bool
opt.OnErr = func(err error) error {
called = true
// existing, nil err not passed
opt.OnError = func(_, _ string, err error) error {
return err
}
err = Copy("test/data/case17", "test/data.copy/case17", opt)
Expect(t, err).ToBe(nil)
Expect(t, called).ToBe(false)

// not existing, process err
opt.OnErr = func(err error) error { return err }
opt.OnError = func(_, _ string, err error) error { return err }
err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt)
Expect(t, os.IsNotExist(err)).ToBe(true)

_, err = os.Stat("test/data.copy/case17/non-existing")
Expect(t, os.IsNotExist(err)).ToBe(true)

// not existing, ignore err
opt.OnErr = func(err error) error { return nil }
opt.OnError = func(_, _ string, err error) error { return nil }
err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt)
Expect(t, err).ToBe(nil)

Expand Down
19 changes: 6 additions & 13 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ func Copy(src, dest string, opts ...Options) error {
opt := assureOptions(src, dest, opts...)
info, err := os.Lstat(src)
if err != nil {
return onError(err, opt)
return onError(src, dest, err, opt)
}
return switchboard(src, dest, info, opt)
}

// switchboard switches proper copy functions regarding file type, etc...
// If there would be anything else here, add a case to this switchboard.
func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) {
defer func() {
err = onError(err, opt)
}()

if info.Mode()&os.ModeDevice != 0 && !opt.Specials {
return err
return onError(src, dest, err, opt)
}

switch {
Expand All @@ -46,7 +42,7 @@ func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) {
err = fcopy(src, dest, info, opt)
}

return err
return onError(src, dest, err, opt)
}

// copyNextOrSkip decide if this src should be copied or not.
Expand Down Expand Up @@ -241,13 +237,10 @@ func fclose(f *os.File, reported *error) {

// onError lets caller to handle errors
// occured when copying a file.
func onError(err error, opt Options) error {
if err == nil {
return nil
}
if opt.OnErr == nil {
func onError(src, dest string, err error, opt Options) error {
if opt.OnError == nil {
return err
}

return opt.OnErr(err)
return opt.OnError(src, dest, err)
}
4 changes: 2 additions & 2 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

// OnErr lets called decide whether or not to continue on particular copy error.
OnErr func(err error) error
OnError func(src, dest string, err error) error

// Skip can specify which files should be skipped
Skip func(srcinfo os.FileInfo, src, dest string) (bool, error)
Expand Down Expand Up @@ -98,7 +98,7 @@ func getDefaultOptions(src, dest string) Options {
return Shallow // Do shallow copy
},
OnDirExists: nil, // Default behavior is "Merge".
OnErr: nil, // Default is "accept error"
OnError: nil, // Default is "accept error"
Skip: nil, // Do not skip anything
AddPermission: 0, // Add nothing
PermissionControl: PerservePermission, // Just preserve permission
Expand Down
2 changes: 1 addition & 1 deletion test/data/case17/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
So if you wanted to ignore error you should add something like this:
```go
opt.OnFileErr = func(_ error) error { return nil }
opt.OnError = func(src, dst string, _ error) error { return nil }
```
The default value is nil and accepts raised error.

0 comments on commit 80eee8d

Please sign in to comment.