runWithConfig method

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

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

Implementation

@override
Future<void> runWithConfig(
  final Configuration<CreateEnvCommandConfig> commandConfig,
) async {
  final projectId = commandConfig.value(CreateEnvCommandConfig.projectId);
  final variableName =
      commandConfig.value(CreateEnvCommandConfig.variableName);
  final variableValue =
      commandConfig.optionalValue(CreateEnvCommandConfig.variableValue);
  final valueFile =
      commandConfig.optionalValue(CreateEnvCommandConfig.valueFile);

  String valueToSet;
  if (variableValue != null) {
    valueToSet = variableValue;
  } else if (valueFile != null) {
    valueToSet = valueFile.readAsStringSync();
  } else {
    throw StateError('Expected one of the value options to be set.');
  }

  final apiCloudClient = runner.serviceProvider.cloudApiClient;

  try {
    await apiCloudClient.environmentVariables.create(
      variableName,
      valueToSet,
      projectId,
    );
  } on Exception catch (e, s) {
    throw FailureException.nested(
        e, s, 'Failed to create a new environment variable');
  }

  logger.success('Successfully created environment variable.');
}