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.get('/operations/${rest.first}') as Map;

    // 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['status'] == 'running') {
      await Future<void>.delayed(const Duration(seconds: 1));
      op = await client.get('/operations/${rest.first}') as Map;
    }

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