run method

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

Runs this command.

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

Implementation

@override
Future<void> run() async {
  config = ConfigService(projectDir);

  final shouldClear = argResults!['clear'] as bool;
  if (shouldClear) {
    await _clearHistory();
    return;
  }

  final clientFilter = argResults!['client'] as String?;
  Logger.jsonMode = argResults!['json'] as bool;
  final limitArg = argResults!['limit'] as String;
  final limit = int.tryParse(limitArg);
  if (limit == null || limit <= 0) {
    throw UsageException(
        '--limit must be a positive integer (got "$limitArg").', usage);
  }

  var entries = await config.loadBuildHistory();

  if (clientFilter != null) {
    entries = entries.where((e) => e['client'] == clientFilter).toList();
  }

  if (argResults!['json'] as bool) {
    stdout.writeln(const JsonEncoder.withIndent('  ')
        .convert(entries.take(limit).toList()));
    return;
  }

  if (entries.isEmpty) {
    Logger.info(
        'No build history recorded yet. Run "udara_cli build" to create some.');
    return;
  }

  Logger.phase('Build History (showing ${entries.length.clamp(0, limit)} '
      'of ${entries.length})');

  for (final entry in entries.take(limit)) {
    _printEntry(entry);
  }
}