func moveOrCopyPath()

in pkg/fileutil/fileutil.go [51:99]


func moveOrCopyPath(moveOrCopy action, destPath, srcPath string, condition func(path string, d fs.DirEntry) (bool, error)) error {
	return filepath.WalkDir(srcPath, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		// Skip the root
		if path == srcPath {
			return nil
		}

		shouldCopy, err := condition(path, d)
		if err != nil {
			return err
		}

		if !shouldCopy {
			if d.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}

		relPath, err := filepath.Rel(srcPath, path)
		if err != nil {
			return err
		}

		dest := filepath.Join(destPath, relPath)

		if moveOrCopy == move {
			if err := os.Rename(path, dest); err != nil {
				return err
			}
			// Rename moves the entire directory, so don't need to continue
			// walking the directory.
			if d.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}

		if d.IsDir() {
			return os.MkdirAll(dest, 0744)
		}

		return CopyFile(dest, path)
	})
}