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() => withApp((app) async {
  final results = argResults!;
  if (app.config.section<AppConfig>().isProduction &&
      !(results['force'] as bool)) {
    stderr.writeln(
      'activitylog:clean deletes records; pass --force to run it in '
      'production.',
    );
    return 1;
  }
  final log = results.rest.firstOrNull;
  final daysOption = results['days'] as String?;
  final days = daysOption == null
      ? ActivityLog.config.deleteRecordsOlderThanDays
      : int.tryParse(daysOption);
  if (days == null) {
    stderr.writeln('The days option must be an integer.');
    return 1;
  }
  stdout.writeln('Cleaning activity log...');
  // Laravel's format, the one both frameworks write in this table.
  final cutoff = formatDateTime(Clock.now().subtract(Duration(days: days)));
  var query = Activity.query().where('created_at', '<', cutoff);
  if (log != null) {
    query = query.inLog(log);
  }
  final deleted = await query.delete();
  stdout
    ..writeln('Deleted $deleted record(s) from the activity log.')
    ..writeln('All done!');
  return 0;
});