run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  final args = argResults!;
  final resolveAll = args.flag(_allFlag);
  final targetIds = [
    for (final restArg in args.rest)
      if (restArg.trim().isNotEmpty) restArg.trim(),
  ];

  if (!resolveAll && targetIds.isEmpty) {
    usageException('Specify at least one comment <id> or pass --all.');
  }

  final siteFilter = _context.resolveOptionalSiteFilter(args);
  final store = _context.resolveStore(args);
  final comments = store.loadComments();
  final now = DateTime.now().toUtc();
  final nowIso = now.toIso8601String();

  if (resolveAll) {
    var updatedCount = 0;
    for (var i = 0; i < comments.length; i++) {
      final item = comments[i];
      if (siteFilter != null && item.site != siteFilter) continue;
      if (item.status != 'resolved') {
        comments[i] = item.copyWith(status: 'resolved', updatedAt: nowIso);
        updatedCount++;
      }
    }
    if (comments.isNotEmpty ||
        Directory(store.reviewDirectoryPath).existsSync()) {
      store.saveComments(comments, now: now);
    }
    final counts = computeCommentCounts(comments);
    _context.out.writeln(
      'Resolved $updatedCount comment${updatedCount == 1 ? '' : 's'}. '
      'Remaining open: ${counts['open']}.',
    );
    return 0;
  }

  final missingIds = <String>[];
  var resolvedCount = 0;
  for (final targetId in targetIds) {
    final idx = comments.indexWhere((item) => item.id == targetId);
    if (idx < 0) {
      missingIds.add(targetId);
      continue;
    }
    if (comments[idx].status != 'resolved') {
      comments[idx] = comments[idx].copyWith(
        status: 'resolved',
        updatedAt: nowIso,
      );
    }
    resolvedCount++;
  }

  if (missingIds.isNotEmpty) {
    _context.err.writeln(
      'Error: Comment ID not found: ${missingIds.join(', ')}.',
    );
    if (resolvedCount == 0) {
      return 1;
    }
  }

  store.saveComments(comments, now: now);
  final counts = computeCommentCounts(comments);
  _context.out.writeln(
    'Resolved $resolvedCount comment${resolvedCount == 1 ? '' : 's'}. '
    'Remaining open: ${counts['open']}.',
  );
  return missingIds.isEmpty ? 0 : 1;
}