execWhich method

Future<String?> execWhich(
  1. String containerName,
  2. String commandName, {
  3. bool ignoreCache = false,
  4. String? def,
})
inherited

Call POSIX which command. Calls exec with command which and args commandName. Caches response than returns the executable path for commandName.

Implementation

Future<String?> execWhich(String containerName, String commandName,
    {bool ignoreCache = false, String? def}) async {
  if (isEmptyString(containerName) || isEmptyString(commandName)) return null;

  if (isEmptyString(commandName, trim: true)) return null;

  commandName = commandName.trim();

  var containerCache =
      _whichCache.putIfAbsent(containerName, () => <String, String>{});
  String? cached;

  if (!ignoreCache) {
    cached = containerCache[commandName];
    if (cached != null) {
      return cached.isNotEmpty ? cached : def;
    }
  }

  var path = await execAndWaitStdoutAsString(
      containerName, 'which', [commandName],
      trim: true, desiredExitCode: 0, dataMatcher: commandName);
  path ??= '';

  containerCache[commandName] = path;

  return path.isNotEmpty ? path : def;
}