run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
final vId = argResults!['variable-id'] as String;
final lId = argResults!['locale-id'] as String;
final field = argResults!['field'] as String;
final text = argResults!['text'] as String;
var id = argResults?['id'] as String? ?? '';
final rawVariant = argResults?['variant'] as String?;
final variant =
(rawVariant != null && rawVariant.isNotEmpty) ? rawVariant : null;
final autoTranslate = argResults?['auto-translate'] == true;
final client = container.read(variableLocalizationServiceClientProvider);
if (id.isEmpty) {
try {
final findReq = VariableLocaleTypeAndVariant(
variableId: vId,
localeId: lId,
fieldName: field,
);
if (variant != null) {
findReq.variant = variant;
}
final existing = await client
.getVariableLocalizationByVariableLocaleTypeAndVariant(
findReq,
options: getCallOptions(requireUser: false),
);
if (existing.id.isNotEmpty) {
id = existing.id;
}
} catch (_) {
// Record does not exist yet; proceed with empty ID to create new entry
}
}
final varLoc = VariableLocalization(
variableId: vId,
localeId: lId,
fieldName: field,
text: text,
autoTranslate: autoTranslate,
);
if (variant != null) {
varLoc.variant = variant;
}
if (id.isNotEmpty) {
varLoc.id = id;
}
final result = await client.upsertVariableLocalization(
varLoc,
options: getCallOptions(requireUser: false),
);
printOutput(result);
return 0;
}