runWithConfig method

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

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

Implementation

@override
Future<void> runWithConfig(
  final Configuration<DbConnectionDetailsOption> commandConfig,
) async {
  final projectId = commandConfig.value(DbConnectionDetailsOption.projectId);

  final apiCloudClient = runner.serviceProvider.cloudApiClient;

  try {
    final connection = await apiCloudClient.database.getConnectionDetails(
      cloudCapsuleId: projectId,
    );

    final portString = connection.port == 5432 ? '' : ':${connection.port}';
    final connectionString =
        'postgresql://${connection.host}$portString/${connection.name}'
        '?sslmode=${connection.requiresSsl ? 'require' : 'disable'}';
    logger.success(
      '''
Connection details:
Host: ${connection.host}
Port: ${connection.port}
Database: ${connection.name}''',
      followUp: '''
This psql command can be used to connect to the database (it will prompt for the password):
psql "$connectionString" --user <username>''',
    );
  } on Exception catch (e, stackTrace) {
    throw FailureException.nested(
        e, stackTrace, 'Failed to get connection details');
  }
}