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;
    await _fanOut(nodes, (node) async {
      final reply = await client.post('/nodes/$node/formula', {
        'formula': formula,
        'action': action,
        'version': ?version,
        if (async) 'async': true,
      });
      if (async) {
        final op = reply as Map;
        return 'dispatched — ops show ${op['id']}';
      }
      final result = (reply as Map)['result'] as Map;
      final ok = result['success'] == true;
      final changed = result['changed'] == true;
      final message = '${result['message'] ?? ''}';
      // 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 = (message.isEmpty || message == 'ok') ? '' : '  $message';
      return '$formula $action: ${ok ? 'ok' : 'FAILED'}'
          '${changed ? ' (changed)' : ''}$detail';
    });
  } finally {
    client.close();
  }
}