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: ops show <id> [--wait]');

  final client = _apiClientFrom(args);
  try {
    var op = await client.operation(rest.first);

    // Polling, deliberately, and only when asked: an operation announces itself
    // finished on the event stream, so a *watcher* has no need to poll — but a
    // script that wants to block on one line does.
    while ((args['wait'] as bool) && op.isRunning) {
      await Future<void>.delayed(const Duration(seconds: 1));
      op = await client.operation(rest.first);
    }

    stdout.writeln(const JsonEncoder.withIndent('  ').convert(op.toJson()));
    if (op.status == OperationStatus.failed) exitCode = 1;
  } finally {
    client.close();
  }
}