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: state set <preset.json> [<node>] [--label …]');
  }
  final file = File(rest.first);
  if (!file.existsSync()) {
    throw CliError('preset file not found: ${rest.first}');
  }
  final preset = jsonDecode(file.readAsStringSync());

  final client = _apiClientFrom(args);
  try {
    final nodes = await _selectNodes(
      client,
      args,
      positional: rest.skip(1).toList(),
    );
    await _fanOut(nodes, (node) async {
      final reply =
          await client.put('/nodes/$node/desired-state', {'preset': preset})
              as Map;
      // Said plainly, because "declared" and "applied" are easy to confuse and
      // the difference is the whole feature.
      return 'declared ${reply['steps']} steps (nothing has run yet)';
    });
  } finally {
    client.close();
  }
}