CopyDir copies a directory from src to dest, recursively copying nested directories
(src, dest string)
| 73 | // CopyDir copies a directory from src to dest, recursively copying nested |
| 74 | // directories |
| 75 | func CopyDir(src, dest string) error { |
| 76 | srcPath := filepath.Clean(src) |
| 77 | destPath := filepath.Clean(dest) |
| 78 | |
| 79 | fi, err := os.Stat(srcPath) |
| 80 | if err != nil { |
| 81 | return errors.Wrap(err, "getting the file info for the input") |
| 82 | } |
| 83 | if !fi.IsDir() { |
| 84 | return errors.New("source is not a directory") |
| 85 | } |
| 86 | |
| 87 | _, err = os.Stat(dest) |
| 88 | if err != nil && !os.IsNotExist(err) { |
| 89 | return errors.Wrap(err, "looking up the destination") |
| 90 | } |
| 91 | |
| 92 | err = os.MkdirAll(dest, fi.Mode()) |
| 93 | if err != nil { |
| 94 | return errors.Wrap(err, "creating destination") |
| 95 | } |
| 96 | |
| 97 | entries, err := os.ReadDir(src) |
| 98 | if err != nil { |
| 99 | return errors.Wrap(err, "reading the directory listing for the input") |
| 100 | } |
| 101 | |
| 102 | for _, entry := range entries { |
| 103 | srcEntryPath := filepath.Join(srcPath, entry.Name()) |
| 104 | destEntryPath := filepath.Join(destPath, entry.Name()) |
| 105 | |
| 106 | if entry.IsDir() { |
| 107 | if err = CopyDir(srcEntryPath, destEntryPath); err != nil { |
| 108 | return errors.Wrapf(err, "copying %s", entry.Name()) |
| 109 | } |
| 110 | } else { |
| 111 | if err = CopyFile(srcEntryPath, destEntryPath); err != nil { |
| 112 | return errors.Wrapf(err, "copying %s", entry.Name()) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | // CopyFile copies a file from the src to dest |
| 121 | func CopyFile(src, dest string) error { |
no test coverage detected