runCommand<T> method

Future<T?> runCommand<T>(
  1. List<String> cmd, {
  2. T? parseResult(
    1. RunResult result
    )?,
  3. T? parseFail(
    1. dynamic error,
    2. StackTrace stack
    )?,
  4. Spinner? spinner,
  5. bool throwOnError = true,
  6. LoggerService? logger,
  7. Duration? timeout = const Duration(seconds: 10),
  8. String label = 'commandRunner',
})

Implementation

Future<T?> runCommand<T>(
  List<String> cmd, {
  T? Function(RunResult result)? parseResult,
  T? Function(dynamic error, StackTrace stack)? parseFail,
  Spinner? spinner,
  bool throwOnError = true,
  LoggerService? logger,
  Duration? timeout = const Duration(seconds: 10),
  String label = 'commandRunner',
}) async {
  spinner?.start();

  try {
    final result = await run(
      cmd,
      timeout: timeout,
    );

    logger?.detail('$label ExitCode: ${result.exitCode}');
    logger?.detail('$label Stdout: ${result.stdout.trim()}');
    logger?.detail('$label Stderr: ${result.stderr}');

    if (throwOnError && result.exitCode != 0) {
      throwToolExit('''
$label failed with exit code ${result.exitCode}:
${result.stdout.trim()}
${result.stderr.trim()}
          ''');
    }

    spinner?.done();

    return parseResult?.call(result);
  } catch (e, s) {
    spinner?.failed();

    logger?.detail('$label Error: $e\n$s');

    return parseFail?.call(e, s);
  }
}