runWithConfig method

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

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

Implementation

@override
Future<void> runWithConfig(
  final Configuration<CreateVariableCommandConfig> commandConfig,
) async {
  final projectId = commandConfig.value(
    CreateVariableCommandConfig.projectId,
  );
  final variableName = commandConfig.value(
    CreateVariableCommandConfig.variableName,
  );
  final variableValue = commandConfig.optionalValue(
    CreateVariableCommandConfig.variableValue,
  );
  final valueFile = commandConfig.optionalValue(
    CreateVariableCommandConfig.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.');
}