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 configPath = super.configPath;
  final strict = argResults!['strict'] as bool;

  final ConfigParser config;
  try {
    config = await ConfigParser.distributeYaml(configPath, globalResults);
  } on ConfigException catch (e) {
    logger.logError(e.message);
    return 1;
  }

  final warnings = <String>[];
  warnings.addAll(_inspectUnknownKeys(configPath, config));

  for (final task in config.tasks) {
    for (final job in task.jobs) {
      final where = "${task.key}.${job.key ?? job.name}";
      warnings.addAll(await _inspectJob(where, job));
    }
  }

  for (final notification in config.notifications) {
    final url = await config.variables.process(notification.webhookUrl);
    if (_hasUnresolvedPlaceholder(url)) {
      warnings.add(
        "notifications > ${notification.provider.name} webhook-url still "
        "contains an unresolved placeholder. Export the secret before running.",
      );
    } else if (!url.startsWith('http://') && !url.startsWith('https://')) {
      warnings.add(
        "notifications > ${notification.provider.name} webhook-url does not "
        "look like an HTTP endpoint.",
      );
    }
  }

  final jobCount =
      config.tasks.fold<int>(0, (sum, task) => sum + task.jobs.length);
  final sep = LogSymbols.separator;
  logger.logInfo(
    ColorizeLogger.dim(
      [
        configPath,
        '${config.tasks.length} task(s)',
        '$jobCount job(s)',
        '${config.notifications.length} notification(s)',
      ].join('  $sep  '),
    ),
  );
  logger.logEmpty();

  if (warnings.isEmpty) {
    logger.logSuccess("no problems found");
    return 0;
  }

  for (final warning in warnings) {
    logger.logWarning(warning);
  }
  logger.logEmpty();

  final tally = "${warnings.length} warning(s)";
  if (strict) {
    logger
        .logError("$tally  ${ColorizeLogger.dim("failing due to --strict")}");
    return 1;
  }
  logger.logWarning(tally);
  return 0;
}