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 until = commandConfig.optionalValue(LogOption.until);
  final since = commandConfig.optionalValue(LogOption.since);
  final tailOpt = commandConfig.optionalValue(LogOption.tail);
  final internalAllOpt = commandConfig.value(LogOption.all);

  DateTime? defaultSince;
  final anyTimeSpanIsSet = until != null || since != null;
  if (internalAllOpt) {
    if (anyTimeSpanIsSet) {
      throw CloudCliUsageException(
        'The --all option cannot be combined with --until or --since.',
      );
    }
  } else if (tailOpt == true) {
    if (anyTimeSpanIsSet) {
      throw CloudCliUsageException(
        'The --tail option cannot be combined with --until or --since.',
      );
    }
  } else if (until != null || since != null) {
    if (until != null && since != null && until.isBefore(since)) {
      throw CloudCliUsageException(
        'The --until value must be after --since value.',
      );
    }
  } else if (until == null && since == null) {
    defaultSince = DateTime.now().subtract(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: until,
      after: since ?? defaultSince,
      limit: limit,
      inUtc: inUtc,
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(e, s, 'Error while fetching log records');
  }
}