github.com/hashicorp/go-getter/v2@v2.2.2/copy_dir.go (about) 1 package getter 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "strings" 8 ) 9 10 // copyDir copies the src directory contents into dst. Both directories 11 // should already exist. 12 // 13 // If ignoreDot is set to true, then dot-prefixed files/folders are ignored. 14 func copyDir(ctx context.Context, dst string, src string, ignoreDot bool, disableSymlinks bool, umask os.FileMode) error { 15 src, err := filepath.EvalSymlinks(src) 16 if err != nil { 17 return err 18 } 19 20 walkFn := func(path string, info os.FileInfo, err error) error { 21 if err != nil { 22 return err 23 } 24 if path == src { 25 return nil 26 } 27 28 if ignoreDot && strings.HasPrefix(filepath.Base(path), ".") { 29 // Skip any dot files 30 if info.IsDir() { 31 return filepath.SkipDir 32 } else { 33 return nil 34 } 35 } 36 37 if disableSymlinks { 38 if info.Mode()&os.ModeSymlink == os.ModeSymlink { 39 return ErrSymlinkCopy 40 } 41 } 42 43 // The "path" has the src prefixed to it. We need to join our 44 // destination with the path without the src on it. 45 dstPath := filepath.Join(dst, path[len(src):]) 46 47 // If we have a directory, make that subdirectory, then continue 48 // the walk. 49 if info.IsDir() { 50 if path == filepath.Join(src, dst) { 51 // dst is in src; don't walk it. 52 return nil 53 } 54 55 if err := os.MkdirAll(dstPath, mode(0755, umask)); err != nil { 56 return err 57 } 58 59 return nil 60 } 61 62 // If we have a file, copy the contents. 63 _, err = copyFile(ctx, dstPath, path, disableSymlinks, info.Mode(), umask) 64 return err 65 } 66 67 return filepath.Walk(src, walkFn) 68 }