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: node logs <id> [-f]');
  final node = rest.first;

  final client = _apiClientFrom(args);
  try {
    final tail = Uri.encodeQueryComponent(args['tail'] as String);
    final lines = (await client.get('/nodes/$node/logs?tail=$tail') as List)
        .cast<Map>();
    if (lines.isEmpty && !(args['follow'] as bool)) {
      stdout.writeln(
        'no logs from $node yet '
        '(the node must run with --ship-logs, which is the default)',
      );
      return;
    }
    for (final line in lines) {
      stdout.writeln(_format(line));
    }
  } finally {
    client.close();
  }

  if (args['follow'] as bool) {
    await _streamSse(
      args,
      '/api/v1/nodes/$node/logs/stream',
      onEvent: (payload) => stdout.writeln(_format(payload)),
    );
  }
}