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

Added a way to skip some files #18

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 9 additions & 1 deletion all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestCopy(t *testing.T) {
})

When(t, "try to copy READ-not-allowed source", func(t *testing.T) {
err := Copy("testdata/case06", "testdata.copy/case06")
err := Copy("testdata/doesNotExist", "testdata.copy/doesNotExist")
Expect(t, err).Not().ToBe(nil)
})

Expand All @@ -138,4 +138,12 @@ func TestCopy(t *testing.T) {
err = os.Chmod(dest, 0755)
Expect(t, err).ToBe(nil)
})

When(t, "try to copy a directory with subdirectories to skip", func(t *testing.T) {
err := CopyButSkipSome("testdata/case06", "testdata.copy/case06", []string{"testdata/case06/caseFilesToSkip"})
Expect(t, err).ToBe(nil)
info, err := os.Stat("./testdata.copy/case06/caseFilesToSkip/README.md")
Expect(t, err).Not().ToBe(nil)
Expect(t, info).ToBe(nil)
})
}
30 changes: 23 additions & 7 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,41 @@ const (
tmpPermissionForDirectory = os.FileMode(0755)
)

// Copy copies src to dest, doesn't matter if src is a directory or a file.
// Copy copies src to dest, doesn't matter if src is a directory or a file
func Copy(src, dest string, opt ...Options) error {
return CopyButSkipSome(src, dest, nil, opt...)
}

// Copy copies src to dest, doesn't matter if src is a directory or a file.
func CopyButSkipSome(src, dest string, toSkip []string, opt ...Options) error {
toSkipMap := make(map[string]struct{})
for i := 0; i < len(toSkip); i++ {
toSkipMap[filepath.FromSlash(toSkip[i])] = struct{}{}
}

opt = append(opt, DefaultOptions)
info, err := os.Lstat(src)
if err != nil {
return err
}
return copy(src, dest, info, opt[0])

return copy(src, dest, toSkipMap, info, opt[0])
}

// copy dispatches copy-funcs according to the mode.
// Because this "copy" could be called recursively,
// "info" MUST be given here, NOT nil.
func copy(src, dest string, info os.FileInfo, opt Options) error {
func copy(src, dest string, toSkip map[string]struct{}, info os.FileInfo, opt Options) error {
if _, isToSkip := toSkip[src]; isToSkip {
return nil
}

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

if info.IsDir() {
return dcopy(src, dest, info, opt)
return dcopy(src, dest, toSkip, info, opt)
}
return fcopy(src, dest, info)
}
Expand Down Expand Up @@ -69,7 +85,7 @@ func fcopy(src, dest string, info os.FileInfo) (err error) {
// dcopy is for a directory,
// with scanning contents inside the directory
// and pass everything to "copy" recursively.
func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {
func dcopy(srcdir, destdir string, toSkip map[string]struct{}, info os.FileInfo, opt Options) (err error) {

originalMode := info.Mode()

Expand All @@ -87,7 +103,7 @@ func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {

for _, content := range contents {
cs, cd := filepath.Join(srcdir, content.Name()), filepath.Join(destdir, content.Name())
if err := copy(cs, cd, content, opt); err != nil {
if err := copy(cs, cd, toSkip, content, opt); err != nil {
// If any error, exit immediately
return err
}
Expand All @@ -114,7 +130,7 @@ func onsymlink(src, dest string, info os.FileInfo, opt Options) error {
if err != nil {
return err
}
return copy(orig, dest, info, opt)
return copy(orig, dest, nil, info, opt)
case Skip:
fallthrough
default:
Expand Down
Empty file added testdata/case06/README.md
Empty file.
Empty file.