run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  final args = argResults!;
  final rest = args.rest;
  if (rest.isEmpty) {
    throw CliError(
      'usage: formula run <formula> [<node>] [--label env=prod | --all] '
      '[--action verify]',
    );
  }
  final formula = rest.first;
  final version = args['formula-version'] as String?;
  final action = args['action'] as String;

  final client = _apiClientFrom(args);
  try {
    final nodes = await _selectNodes(
      client,
      args,
      positional: rest.skip(1).toList(),
    );
    final async = args['async'] as bool;
    final parsed = FormulaAction.parse(action);
    await _fanOut(nodes, (node) async {
      if (async) {
        final operation = await client.runFormulaAsync(
          node,
          formula: formula,
          action: parsed,
          version: version,
        );
        return 'dispatched — ops show ${operation.id}';
      }
      final result = (await client.runFormula(
        node,
        formula: formula,
        action: parsed,
        version: version,
      )).result;
      // The formula's own message is worth showing when it says something the
      // verdict does not — a failure's reason, or a version. "ok  ok" is not.
      final detail = (result.message.isEmpty || result.message == 'ok')
          ? ''
          : '  ${result.message}';
      return '$formula $action: ${result.success ? 'ok' : 'FAILED'}'
          '${result.changed ? ' (changed)' : ''}$detail';
    });
  } finally {
    client.close();
  }
}