runWithConfig method

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

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

Implementation

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

  final apiCloudClient = runner.serviceProvider.cloudApiClient;

  late CustomDomainNameWithDefaultDomains customDomainNameWithDefaultDomains;

  try {
    customDomainNameWithDefaultDomains = await apiCloudClient.customDomainName
        .add(
          domainName: domainName,
          target: target,
          cloudCapsuleId: projectId,
        );
  } on Exception catch (e, stackTrace) {
    throw FailureException.nested(
      e,
      stackTrace,
      'Could not add the custom domain',
    );
  }

  logger.success('Custom domain attached successfully!', newParagraph: true);

  final targetDefaultDomain =
      customDomainNameWithDefaultDomains.defaultDomainsByTarget[target];

  if (targetDefaultDomain == null) {
    throw FailureException(
      error: 'Could not find the target domain for "$target".',
    );
  }

  if (DomainUtils.isSubDomain(domainName)) {
    _logDomainInstructions(
      logger: logger,
      domainName: domainName,
      projectId: projectId,
      records: [
        (type: 'CNAME', domain: domainName, value: targetDefaultDomain),
      ],
    );
    return;
  }

  final wwwRedirectEnabled = target == DomainNameTarget.web;

  final records = <({String type, String domain, String value})>[
    (type: 'ANAME', domain: domainName, value: targetDefaultDomain),
    (
      type: 'TXT',
      domain: domainName,
      value: customDomainNameWithDefaultDomains
          .customDomainName
          .dnsRecordVerificationValue,
    ),
    if (wwwRedirectEnabled)
      (type: 'CNAME', domain: 'www.$domainName', value: targetDefaultDomain),
  ];

  _logDomainInstructions(
    logger: logger,
    domainName: domainName,
    projectId: projectId,
    records: records,
  );
}