setVariable static method
Implementation
static Future<void> setVariable(
final Client cloudApiClient, {
required final CommandLogger logger,
required final String baseCommand,
required final String projectId,
required final String name,
required final String value,
final bool? secret,
}) async {
_validateName(baseCommand, name);
final listed = await _fetch(cloudApiClient, projectId);
final existingStore = _storeOf(
name,
unmasked: listed.unmasked,
secrets: listed.secrets,
);
if (existingStore == _VariableStore.unmasked && secret == true) {
throw FailureException(
error:
'"$name" already exists as an unmasked variable. '
'To recreate it as a secret:',
hint:
'$baseCommand variable unset $name\n'
' $baseCommand variable set --secret $name <value>',
);
}
if (existingStore == _VariableStore.secret && secret == false) {
throw FailureException(
error:
'"$name" already exists as a secret. '
'To recreate it as an unmasked variable:',
hint:
'$baseCommand variable unset $name\n'
' $baseCommand variable set --no-secret $name <value>',
);
}
final store =
existingStore ??
(secret == true ? _VariableStore.secret : _VariableStore.unmasked);
try {
switch (store) {
case _VariableStore.unmasked:
if (existingStore == null) {
await cloudApiClient.environmentVariables.create(
name,
value,
projectId,
);
} else {
await cloudApiClient.environmentVariables.update(
name: name,
value: value,
cloudCapsuleId: projectId,
);
}
case _VariableStore.secret:
if (existingStore == null) {
await cloudApiClient.secrets.create(
secrets: {name: value},
cloudCapsuleId: projectId,
);
} else {
await cloudApiClient.secrets.upsert(
secrets: {name: value},
cloudCapsuleId: projectId,
);
}
}
} on Exception catch (e, s) {
throw FailureException.nested(
e,
s,
'Failed to set the environment variable',
);
}
logger.success(
store == _VariableStore.secret
? 'Successfully set secret: $name.'
: 'Successfully set environment variable: $name.',
);
}