run method

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

Runs this command.

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

Implementation

@override
Future<ExitCode> run() async {
  final query = switch (argResults?.rest) {
    [final String query, ...] => query,
    final Iterable<String> all when all.isNotEmpty => all.join(' '),
    _ => null,
  };

  final content = scriptsYaml.scripts();
  if (content == null) {
    logger.err('No ${ScriptsYaml.fileName} file found');
    return ExitCode.noInput;
  }

  final scriptConfig = ScriptsConfig.fromJson(content);

  if (query != null) {
    final result = scriptConfig.search(query);

    if (result.isEmpty) {
      logger.err('No scripts found for query: $query');
      return ExitCode.noInput;
    }

    logger
      ..detail('Found ${result.length} scripts for query: $query')
      ..write('\n');

    for (final script in result) {
      final details = script.printDetails().trim();
      if (details.isEmpty) continue;

      logger.info(details);
    }
  } else {
    logger
      ..write('\n')
      ..info(
        scriptConfig.listOut(
          wrapCallableKey: (s) => lightGreen.wrap(s) ?? s,
          wrapNonCallableKey: (s) => cyan.wrap(s) ?? s,
          wrapMeta: (s) => lightBlue.wrap(s) ?? s,
        ),
      );
  }

  return ExitCode.success;
}