resolveDockerBinaryPath static method

Future<String> resolveDockerBinaryPath()

Resolves the full path of the Docker binary. If fails to resolve, returns docker.

Implementation

static Future<String> resolveDockerBinaryPath() async {
  late final String findCmd;
  if (Platform.isWindows) {
    findCmd = 'where';
  } else {
    findCmd = 'which';
  }

  var processResult = await Process.run(findCmd, <String>['docker'],
      stdoutEncoding: systemEncoding);

  if (processResult.exitCode == 0) {
    var output = processResult.stdout as String?;
    output ??= '';
    output = output.trim();

    if (output.isNotEmpty) {
      if (Platform.isWindows) {
        output = output
            .split('\n')
            .where((element) => element.endsWith('exe'))
            .first
            .replaceAll(RegExp(r'/'), r'\'); // replace file separator
      }
      return output;
    }
  }

  return 'docker';
}