Skip to content

Commit

Permalink
Merge pull request #25 from otiai10/refactor/do-not-skip-root
Browse files Browse the repository at this point in the history
Refactor/do not skip root
  • Loading branch information
otiai10 committed May 24, 2020
2 parents 26515b4 + d54013a commit c5f0361
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
22 changes: 11 additions & 11 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ func TestCopy(t *testing.T) {
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)

var skipErr = errors.New("skip err")

opt = Options{Skip: func(src string) (bool, error) {
return false, skipErr
}}
err = Copy("testdata/case06", "testdata.copy/case06.01", opt)
Expect(t, err).ToBe(skipErr)

files, err := ioutil.ReadDir("./testdata.copy/case06.01")
Expect(t, err).ToBe(nil)
Expect(t, len(files)).ToBe(0)
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) {
return false, errInsideSkipFunc
}}
err := Copy("testdata/case06", "testdata.copy/case06.01", opt)
Expect(t, err).ToBe(errInsideSkipFunc)
files, err := ioutil.ReadDir("./testdata.copy/case06.01")
Expect(t, err).ToBe(nil)
Expect(t, len(files)).ToBe(0)
})
})

When(t, "Options.AddPermission provided", func(t *testing.T) {
Expand Down
49 changes: 23 additions & 26 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,34 @@ func Copy(src, dest string, opt ...Options) error {
if err != nil {
return err
}
options := assure(opt...)
return copy(src, dest, info, false, options)
return switchboard(src, dest, info, assure(opt...))
}

// copy dispatches copy-funcs according to the mode.
// 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) error {
switch {
case info.Mode()&os.ModeSymlink != 0:
return onsymlink(src, dest, info, opt)
case info.IsDir():
return dcopy(src, dest, info, opt)
default:
return fcopy(src, dest, info, opt)
}
}

// copy decide if this src should be copied or not.
// Because this "copy" could be called recursively,
// "info" MUST be given here, NOT nil.
func copy(src, dest string, info os.FileInfo, skip bool, opt Options) error {

func copy(src, dest string, info os.FileInfo, opt Options) error {
skip, err := opt.Skip(src)
if err != nil {
return err
}
if skip {
return nil
}

if info.Mode()&os.ModeSymlink != 0 {
return onsymlink(src, dest, info, opt)
}

if info.IsDir() {
return dcopy(src, dest, info, opt)
}
return fcopy(src, dest, info, opt)
return switchboard(src, dest, info, opt)
}

// fcopy is for just a file,
Expand Down Expand Up @@ -98,16 +105,10 @@ func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {
return
}

var skip bool
for _, content := range contents {
cs, cd := filepath.Join(srcdir, content.Name()), filepath.Join(destdir, content.Name())

skip, err = opt.Skip(cs)
if err != nil {
return
}

if err = copy(cs, cd, content, skip, opt); err != nil {
if err = copy(cs, cd, content, opt); err != nil {
// If any error, exit immediately
return
}
Expand All @@ -130,11 +131,7 @@ func onsymlink(src, dest string, info os.FileInfo, opt Options) error {
if err != nil {
return err
}
skip, err := opt.Skip(orig)
if err != nil {
return err
}
return copy(orig, dest, info, skip, opt)
return copy(orig, dest, info, opt)
case Skip:
fallthrough
default:
Expand Down

0 comments on commit c5f0361

Please sign in to comment.