run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() => withApp((app) async {
  final client = Redis.connection(_connection(this));
  final write = argResults!['write'] == true;
  Future<bool> execute(List<String> args) async {
    if (!write && !isReadOnlyRedisCommand(args)) {
      stderr.writeln(
        '${args.first.toUpperCase()} is not a read-only command; '
        'run redis:cli --write to allow it.',
      );
      return false;
    }
    try {
      stdout.writeln(formatRedisReply(await client.command(args)));
      return true;
    } on RedisCommandException catch (error) {
      stdout.writeln(formatRedisReply(error));
      return false;
    }
  }

  final rest = argResults!.rest;
  if (rest.isNotEmpty) {
    return await execute(rest) ? 0 : 1;
  }
  stdout.writeln(
    'Connected to ${client.config.redacted}'
    '${write ? '' : ' (read-only)'}; quit to leave.',
  );
  while (true) {
    stdout.write('redis> ');
    final line = stdin.readLineSync();
    if (line == null || line.trim().toLowerCase() == 'quit') {
      return 0;
    }
    try {
      final args = parseRedisCommandLine(line);
      if (args.isNotEmpty) {
        await execute(args);
      }
    } on FormatException catch (error) {
      stderr.writeln(error.message);
    }
  }
});