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 query = <String>[
    for (final label in args['label'] as List<String>)
      'label=${Uri.encodeQueryComponent(label)}',
    if (args['offline'] as bool)
      'online=false'
    else if (args.wasParsed('online'))
      'online=${args['online']}',
  ].join('&');

  final client = _apiClientFrom(args);
  try {
    final nodes =
        (await client.get('/nodes${query.isEmpty ? '' : '?$query'}') as List)
            .cast<Map>();
    if (nodes.isEmpty) {
      stdout.writeln(
        query.isEmpty ? 'no nodes registered' : 'no node matches',
      );
      return;
    }
    stdout.writeln('NODE                 ONLINE  PLATFORM   LABELS');
    for (final n in nodes) {
      final id = (n['nodeId'] as String).padRight(20);
      final online = (n['online'] as bool? ?? false) ? 'yes   ' : 'no    ';
      final platform = '${(n['platform'] as Map?)?['osName'] ?? '?'}'
          .padRight(10);
      final labels = (n['labels'] as Map?) ?? const {};
      final rendered = labels.entries
          .map((l) => '${l.key}=${l.value}')
          .join(' ');
      stdout.writeln('$id $online  $platform $rendered');
    }
  } finally {
    client.close();
  }
}