runWithConfig method

  1. @override
Future<void> runWithConfig(
  1. Configuration<LogOption> commandConfig
)
override

Runs this command with prepared configuration (options). Subclasses should override this method.

Implementation

@override
Future<void> runWithConfig(
  final Configuration<LogOption> commandConfig,
) async {
  final projectId = commandConfig.value(LogOption.projectId);
  final limit = commandConfig.value(LogOption.limit);
  final inUtc = commandConfig.value(LogOption.utc);
  final recentOpt = commandConfig.optionalValue(LogOption.recent);
  final beforeOpt = commandConfig.optionalValue(LogOption.before);
  final afterOpt = commandConfig.optionalValue(LogOption.after);
  final tailOpt = commandConfig.optionalValue(LogOption.tail);
  final internalAllOpt = commandConfig.value(LogOption.all);

  final DateTime? before, after;
  final anyTimeSpanIsSet =
      recentOpt != null || beforeOpt != null || afterOpt != null;
  if (internalAllOpt) {
    if (anyTimeSpanIsSet) {
      throw CloudCliUsageException(
        'The --all option cannot be combined with '
        '--before, --after, or --recent.',
      );
    }

    before = null;
    after = null;
  } else if (tailOpt == true) {
    if (anyTimeSpanIsSet) {
      throw CloudCliUsageException(
        'The --tail option cannot be combined with '
        '--before, --after, or --recent.',
      );
    }

    before = null;
    after = null;
  } else if (beforeOpt != null || afterOpt != null) {
    if (recentOpt != null) {
      throw CloudCliUsageException(
        'The --recent option cannot be combined with '
        '--before or --after.',
      );
    }

    before = beforeOpt;
    after = afterOpt;
    if (before != null && after != null && before.isBefore(after)) {
      throw CloudCliUsageException(
        'The --before value must be after --after value.',
      );
    }
  } else {
    // If no range specified, default to fetch recent logs
    before = null;
    after = DateTime.now().subtract(recentOpt ?? Duration(minutes: 10));
  }

  if (tailOpt == true) {
    try {
      await LogsFeature.tailContainerLog(
        runner.serviceProvider.cloudApiClient,
        writeln: logger.line,
        projectId: projectId,
        limit: limit,
        inUtc: inUtc,
      );
    } on Exception catch (e, s) {
      throw FailureException.nested(e, s, 'Error while tailing log records');
    }

    return;
  }

  try {
    await LogsFeature.fetchContainerLog(
      runner.serviceProvider.cloudApiClient,
      writeln: logger.line,
      projectId: projectId,
      before: before,
      after: after,
      limit: limit,
      inUtc: inUtc,
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(e, s, 'Error while fetching log records');
  }
}