Copy copies a file to a destination path
(sourcePath string, targetPath string, overwrite bool)
| 65 | |
| 66 | // Copy copies a file to a destination path |
| 67 | func Copy(sourcePath string, targetPath string, overwrite bool) error { |
| 68 | if overwrite { |
| 69 | return recursiveCopy.Copy(sourcePath, targetPath) |
| 70 | } |
| 71 | |
| 72 | var err error |
| 73 | |
| 74 | // Convert to absolute path |
| 75 | sourcePath, err = filepath.Abs(sourcePath) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | // Convert to absolute path |
| 81 | targetPath, err = filepath.Abs(targetPath) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | return filepath.Walk(sourcePath, func(nextSourcePath string, fileInfo os.FileInfo, err error) error { |
| 87 | nextTargetPath := filepath.Join(targetPath, strings.TrimPrefix(nextSourcePath, sourcePath)) |
| 88 | if fileInfo == nil { |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | if !fileInfo.Mode().IsRegular() { |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | if fileInfo.IsDir() { |
| 97 | _ = os.MkdirAll(nextTargetPath, os.ModePerm) |
| 98 | return Copy(nextSourcePath, nextTargetPath, overwrite) |
| 99 | } |
| 100 | |
| 101 | _, statErr := os.Stat(nextTargetPath) |
| 102 | if statErr != nil { |
| 103 | return recursiveCopy.Copy(nextSourcePath, nextTargetPath) |
| 104 | } |
| 105 | |
| 106 | return nil |
| 107 | }) |
| 108 | } |