runCommand method

  1. @override
Future<String?> runCommand(
  1. String binaryPath,
  2. List<String> args, {
  3. bool sudo = false,
  4. int? expectedExitCode,
})
override

Runs a command using the specified binary path and arguments.

  • binaryPath: The path to the binary to execute.
  • args: A list of arguments to pass to the command.
  • sudo: Whether the command should be run with sudo privileges. Defaults to false.
  • expectedExitCode: The expected exit code from the command. If not provided accepts any exit code.

Returns a Future that completes with the command's output as a String.

Implementation

@override
Future<String?> runCommand(String binaryPath, List<String> args,
    {bool sudo = false, int? expectedExitCode}) async {
  if (verbose) {
    print('-- RUN> ${sudo ? 'sudo ' : ''}$binaryPath ${args.join(' ')}');
  }

  final result = sudo
      ? await Process.run('sudo', [binaryPath, ...args])
      : await Process.run(binaryPath, args);

  if (verbose) {
    print('-- exitCode: ${result.exitCode}');
  }

  if (expectedExitCode != null && result.exitCode != expectedExitCode) {
    return null;
  }

  final output = result.stdout as String? ?? '';

  if (verbose) {
    print('<<<\n$output>>>');
  }

  return output;
}