handleMove method

Future<void> handleMove(
  1. String srcPath,
  2. String destPath
)

Implementation

Future<void> handleMove(String srcPath, String destPath) async {
  await _prepareClient();
  final src = await client.resolvePath(srcPath);

  Map<String, dynamic>? destParent;
  String destName;
  bool isRename;

  try {
    final destObj = await client.resolvePath(destPath);
    if (destObj['type'] == 'folder') {
      destParent = destObj;
    } else {
      _exit('Destination exists as a file.');
    }
    final plan = planMove(
        srcPath: srcPath, destPath: destPath, destIsExistingFolder: true);
    destName = plan.destName;
    isRename = plan.isRename;
  } catch (_) {
    final parentDir = p.dirname(destPath);
    try {
      destParent =
          await client.resolvePath(parentDir == '.' ? '/' : parentDir);
      if (destParent['type'] != 'folder') throw Exception('Parent not dir');
    } catch (e) {
      _exit('Destination parent not found.');
    }
    final plan = planMove(
        srcPath: srcPath, destPath: destPath, destIsExistingFolder: false);
    destName = plan.destName;
    isRename = plan.isRename;
  }

  print(
      '\ud83d\ude9a Moving "${src['path']}" to "${destParent['path']}/$destName"...');

  if (src['parent'] != destParent['uuid']) {
    await client.moveItem(src['uuid'], destParent['uuid'], src['type']);
  }

  final currentName = p.basename(src['path']!);
  if (isRename && destName != currentName) {
    await client.renameItem(src['uuid'], destName, src['type']);
  }

  print('\u2705 Done.');
}